Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex and the OR operator without grouping in Python?

Tags:

Here are the cases. I'm looking for the following pattern in a log file.

All strings are in the form of AB_N or CDE_N. AB and CDE are fixed letters, followed by an underscore. N can be either 2 or 3 numbers.

I tried (AB|CDE)_\d{2,3} but that returns a group. I can't do \w{2,3}\d{2,3} because it has to be either AB or CDE and not AC or FEG. Omitting the parentheses breaks too. I am sure the solution is simple but I'm new to python and regex and can't figure this out.

like image 497
pedram Avatar asked Dec 20 '12 03:12

pedram


People also ask

What is non capturing group in regex Python?

Code language: Python (python) This syntax captures whatever match X inside the match so that you can access it via the group() method of the Match object. Sometimes, you may want to create a group but don't want to capture it in the groups of the match.

What is non capturing group in regex?

tl;dr non-capturing groups, as the name suggests are the parts of the regex that you do not want to be included in the match and ?: is a way to define a group as being non-capturing. Let's say you have an email address [email protected] . The following regex will create two groups, the id part and @example.com part.

WHAT IS group in regex Python?

What is Group in Regex? A group is a part of a regex pattern enclosed in parentheses () metacharacter. We create a group by placing the regex pattern inside the set of parentheses ( and ) . For example, the regular expression (cat) creates a single group containing the letters 'c', 'a', and 't'.

Why do we use group () in Python?

groups() method. This method returns a tuple containing all the subgroups of the match, from 1 up to however many groups are in the pattern. The default argument is used for groups that did not participate in the match; it defaults to None.


1 Answers

A ?: inside a parenthesis in a regex makes it non-capturing. Like so: (?:AB|CDE)_\d{2,3}

See docs here: http://docs.python.org/3/library/re.html About a third of the way through it goes over the non-capturing syntax.

like image 181
mattmc3 Avatar answered Oct 23 '22 16:10

mattmc3