Skip to main content

Posts

Showing posts with the label Regex

C# Regex replace all occurrences of 3 numbers together with a different one from a list

I have a list of numbers that have in the left the new number and in the right the old number I want to change, each separated by a Tab character: 000 256 007 002 056 078 And I have filenames with the following composition: aaaa_bbb_01_cccc_000_a aaaa_bbb_01_cccc_000_b aaaa_bbb_01_cccc_000_c aaaa_bbb_01_cccc_007_a aaaa_bbb_01_cccc_056_a I want to change the 3 digits that are together into it's corresponding new number, but the fact that some of the numbers in the filenames repeat have me a little stuck since I'm just trying to learn regex. The output I want would look like this (keeping the same order as above): aaaa_bbb_01_cccc_256_a aaaa_bbb_01_cccc_256_b aaaa_bbb_01_cccc_256_c aaaa_bbb_01_cccc_002_a aaaa_bbb_01_cccc_078_a How can I change these filenames using regex? If it's not possible using regex what other alternative could I use? by Claudia Provoste in StackOverflow on July 05, 2022 . Answer Extra...

Find first match in string from the end

I have a string Manager of Medical Threat Devlop at Micro by regular expression findall i want to find any words which goes after "at","for", "of", . If to apply r'(?:for|at|of)\s+(.*)' i will get incorrect ['Medical Threat Devlop at Micro'] i want find by regular expression from the end of string ['Micro'] P/S. Please don't advice split More examples: 1)Manager of Medical Threat Devlop at Canno -> Canno 2)Manager of Medicalof Threat Devlop of Canno -> Canno 3)Manager of Medicalfor Threat Devlop for Canno -> Canno 4)Threat Devlop at Canno Matt -> Canno Matt by Vladimir Shabuniayeu in StackOverflow on July 05, 2022 . Answer You can use re.findall(r'.*\b(?:for|at|of)\s+(.*)', text) See the regex demo . Details : .* - any zero or more chars other than line break chars, as many as possible \b - a word boundary (?:for|at|of) - for...

Regex .NET OR operator not working on alternative group

I need a regex which validate string of numbers either math "aabb" or "abba" pattern. For example: both 1122 or 1221 is valid Regex for both "aabb", "abba" worked fine alone. But when i'm trying to combine "aabb" OR "abba", the result of "aabb" is always false. (1122 returned not valid) Here is my implementation in C#: string phoneNumber = "1221"; // "1122" failed Dictionary<string, string> subPatterns = new Dictionary<string, string>(); subPatterns[@"(\d)(\d)\2\1$"] = "abba"; subPatterns[@"(\d)\1(\d)\2$"] = "aabb"; string pattern = string.Join("|", subPatterns.Select(e => e.Key)); foreach (Match m in Regex.Matches(phoneNumber, pattern)) { if (m.Success) { Console.WriteLine("TRUE"); } } Did i missed something? by Minh LĂȘ in ...

re get text between braces

Data module All { obj Bucket { Damage = 900 Type = Weapon Cooldown = 2 Craftable = false } } Output "Bucket": { Damage: 900 Type: Weapon Cooldown: 2 Craftable: false } Current Code def get_object(x): pattern = r'\{+(.*?)\}' return re.findall(pattern, x, re.MULTILINE) # returns [] I want to get the data between the { }, this code works for eg. "{ text }" but not for this string, maybe it's because of the newlines, I don't know much regex so any help is appreciated! by Lun in StackOverflow on 2022-06-05 . Answer Use this pattern (?<=obj\s)(\w+)(?:.*?)(\{.*?\}) Also, see the demo Python Example import re text = """module All { obj Bucket { Damage = 900 Type = Weapon Cooldown = 2 Craftable = false } }""" result = re.search(r"(?<=obj\s...

Combine regex character classes

Regex pattern /[^[:ascii:]]+/ui will match one or more non-ascii characters. Regex pattern /[\p{L}]+/ui will match one or more characters in unicode 'letter' class. I can't figure out a way how to match one or more characters that are in unicode 'letter' class AND are not ascii characters. by Karolis in StackOverflow on 2022-06-05 . Answer You can use a negated character class like this: [^\P{L}[:ascii:]]+ RegEx Demo 1 This will match 1+ of any character that is not an ASCII and not matched by \P{L} (inverse of \p{L} ) Alternatively, you can use negative lookahead in a non-capture group: (?:(?![[:ascii:]])\p{L})+ RegEx Demo 2 by anubhava on 2022-06-05 . Other helpful answers You can use [^\P{L}A-Za-z]+ It matches any Unicode letter that is not equal to ASCII letter. See the regex demo . In PHP, you should use the u flag to make it work correctly with ...