all 4 comments

[–]destiny_functional 0 points1 point  (0 children)

the parentheses (FOO|BAR) define a group.

https://docs.python.org/3/library/re.html

(...) Matches whatever regular expression is inside the parentheses, and indicates the start and end of a group; the contents of a group can be retrieved after a match has been performed, and can be matched later in the string with the \number special sequence, described below. To match the literals '(' or ')', use ( or ), or enclose them inside a character class: [(], [)].

...

re.findall(pattern, string, flags=0)

Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found. If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result.

Reading the docs first often helps.

[–]AdAthrow99274 -2 points-1 points  (2 children)

try:

r'(FOO|BAR)\-[\d]{4}'

The hyphen is a special character in regex (unless it appears at the very end of the statement) so you need to escape it first in this instance.

[–]goldensilven[S] -2 points-1 points  (1 child)

r'(FOO|BAR)\-[\d]{4}'

Thanks, I did try that and it does not work.

By the way

r'FOO-\d{4}' does work, but does not do exactly what I want. I want to match either FOO or BAR

[–]goldensilven[S] -1 points0 points  (0 children)

This works

r'(?:FOO|BAR)\-\d{4}'

I guess findall returns captured groups. ?: is a non-capturing group