1

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?

1
  • 1
    What do you want returned if the string were "s = string.charAt (12) != 'abc' + 'def'"? You need to state the rule for identifying the characters you wish to extract. Commented Jul 9, 2020 at 19:43

4 Answers 4

2

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.

Pictorial Representation

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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')

Comments

1

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

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.