Why doesn't the regex match when I add groups?

I have this regex code in python:

if re.search(r"\{\\fad|fade\(\d{1,4},\d{1,4}\)\}", text):
  print(re.search(r"\{\\fad|fade\((\d{1,4}),(\d{1,4})\)\}", text).groups())

textis {\fad(200,200)}Épisode 101 : {\i1}The Ghost{\i0}\Nv. 1.03and is read from a file (I do not know if this helps).

This returns the following:

(None, None)

When I change the regular expression in print to r"\{\\fad\((\d{1,4}),(\d{1,4})\)\}", it returns the correct values:

(200, 200)

Can anyone understand why the conditional fad|fadematches the regular expression in re.searchbut does not return the correct group values ​​in print?

Thanks.

+3
source share
6 answers

Place additional parades around the selection: re.search(r"{(?:\\fad|fade)\((\d{1,4}),(\d{1,4})\)}", text).groups()

, {} , .

+6

, , "{fad", "fade (..." ). fad | fade . :

r"\{\\(?:fad|fade)\(\d{1,4},\d{1,4}\)\}"

[] , if, , , , , "{\ fad]. . , , .

+4

:

r"\{\\fade?\(\d{1,4},\d{1,4}\)\}"
+2

, "\ fad" "fade", , \ , "\ fad" "\ fade".

+1
source

Try this instead:

r"\{\\fade?\((\d{1,4}),(\d{1,4})\)\}"

e?is optional e. The way you have it now matches {\fadorfade(0000,0000)}

+1
source

I don’t know the python dialect of regular expressions, but you don’t need to “group” “fad | fade” in any way to make sure it is not trying to find “fad or fade” (etc.) .. "

0
source

Source: https://habr.com/ru/post/1706557/


All Articles