Home > Back-end >  Regex python Match after and before a specific string
Regex python Match after and before a specific string

Time:12-03

Lets say we have this string:"Code:1,Some text some other text {fdf: more text, attr=important "

I want to catch the pattern using Regex that can findall attr and extract important and 1 and put them in dict.

I tried this one:

(?<=testcaseid_)[^_] _[^_] 

but still capture all the previous

CodePudding user response:

import re

string = "Code:1,Some text some other text {fdf: more text, attr=important"
regex = r"Code:(\d ),[^\{]*\{[^\:]*\:.*attr=(\w )"

# Find all matches
matches = re.findall(regex, string)

# Create a dictionary from the matches
result = {
    "code": matches[0][0],
    "attr": matches[0][1]
}

# Print the result
print(result)

Hope this helps!

CodePudding user response:

I'm not sure if I understand well, but if you want to get everything starts from "1" to something after attr= you can also use regex like this:

r"1.*?attr=\w "
  • Related