'PyCharm Regex works not as expected (as other services) [duplicate]
i'm struggling to debug my python code with regex in PyCharm.
The idea: I want to find any case of 'here we are', which can go with or without 'attention', and the word 'attention' can be separated by whitespace, dot, comma, exclamation mark.
I expect this expression should do the job
r'(attention.{0,2})?here we are'
Online services like https://regex101.com/ and https://pythex.org/ confirm my expression is correct – and i'm getting expected "attention! here we are"
However, if i run the below code in PyCharm I'm getting such (unexpected for me) result.
my_string_1 = 'attention! here we are!'
my_list = re.findall(r'(attention.{0,2})?here we are', my_string_1)
print(my_list)
>>> ['attention! ']
Could someone direct me to the reason why PyCharm's outcome is different? Thanks
Solution 1:[1]
If there are any capturing groups in the match, re.findall will return them instead of the entire match. Thus, only the content matched with (attention.{0,2}) will be returned.
In order to avoid that, you can use a non-capturing group instead.
r'(?:attention.{0,2})?here we are'
Solution 2:[2]
The following will give you a similar answer to the online tools:
pat = '(attention.{0,2})?here we are'
x = re.search(pat, my_string_1)
print(x.group())
attention! here we are
search vs findall work differently. Not something that was immediately obvious when I first started learning regex.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Cubix48 |
| Solution 2 | Marcel Wilson |
