Reputation: 9
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?
Upvotes: 1
Views: 342
Reputation: 9
Thanks everyone for the very helpful answer. My problem has been solved ^^
Upvotes: -1
Reputation: 18611
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
Upvotes: 1
Reputation:
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.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.
Upvotes: 2
Reputation: 111
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')
Upvotes: 1