Python regex - Ignore parentheses as indexing?

I have currently written a nooby regex pattern that assumes excessive use of the characters "(" and ")", but I use them for "or" operators such as (A | B | C), which means A or B or C .

I need to find each pattern match in a string.
Attempting to use the re.findall(pattern, text) method is re.findall(pattern, text) because it interprets the brackets as indexing indices (or something else that is correct jargon), and therefore each item in the list is not a string displaying consistent text sections, but instead, is a tuple (which contains very ugly fragments of pattern matching).

Is there an argument that I can pass to findall to ignore paranthesis as an index?
Or I will have to use a very ugly combination of re.search and re.sub

(This is the only solution I can think of: find the re.search index, add a consistent section of text to the List, then remove it from the original line {using ugly index tricks}, continuing this until there are no more matches. Obviously , this is terrible and undesirable).

Thanks!

+6
source share
2 answers

Yes, add ?: group to make it not exciting.

 import re print re.findall('(.(foo))', "Xfoo") # [('Xfoo', 'foo')] print re.findall('(.(?:foo))', "Xfoo") # ['Xfoo'] 

See re syntax for more details.

+9
source
 re.findall(r"(?:A|B|C)D", "BDE") 

or

 re.findall(r"((?:A|B|C)D)", "BDE") 
+1
source

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


All Articles