简体   繁体   中英

The problem of regex strings containing special characters in python

I have a string: " s = string.charAt (0) == 'd' "
I want to retrieve a tuple of ('0', 'd')
I have used: re.search(r "\ ((. ?) \) == '(.?)' && "," string.charAt (0) == 'd' ")
I checked the s variable when printed as "\\ ((.?) \\) == '(.?) '&& "
How do I fix it?

You may try:

\((\d+)\).*?'(\w+)'

Explanation of the above regex:

  • \( - Matches a ( literally.
  • (\d+) - Represents the first capturing group matching digits one or more times.
  • \) - Matches a ) literally.
  • .*? - Lazily matches everything except a new-line.
  • '(\w+)' - Represents second capturing group matching ' along with any word character( [0-9a-zA-Z_] ) one or more times.

图示

Regex Demo

import re
regex = r"\((\d+)\).*?'(\w+)'"
test_str = "s = string.charAt (0) == 'd'"
print(re.findall(regex, test_str))
# Output: [('0', 'd')]

You can find the sample run of the above implementation in here.

Use

\((.*?)\)\s*==\s*'(.*?)'

See proof . The first variable is captured inside Group 1 and the second variable is inside Group 2.

Python code:

import re
string = "s = string.charAt (0) == 'd'"
match_data = re.search(r"\((.*?)\)\s*==\s*'(.*?)'", string)
if match_data:
    print(f"Var#1 = {match_data.group(1)}\nVar#2 = {match_data.group(2)}")

Output:

Var#1 = 0
Var#2 = d

Your regular expression should be ".*\((.?)\).* '(.?)\'" . This will get both the character inside the parenthesis and then the character inside the single quotes.

>>> import re
>>> s = " s = string.charAt (0) == 'd'"
>>> m = re.search(r".*\((.?)\) .* '(.?)'", s)
>>> m.groups()
('0', 'd')

Thanks everyone for the very helpful answer. My problem has been solved ^^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM