A similar approach to the existing ones in OP's question is this one:
import re
string1 = "234_are223422.txt"
string2 = "234_are223422_nextdate_2210.txt"
regex = re.compile(r"\w+(?:nextdate\w+)?\.txt")
re.search(regex, string1).group() # outputs "234_are223422.txt"
re.search(regex, string2).group() # outputs "234_are223422_nextdate_2210.txt"
\w+ is very broad. Consider using \d+_are\d+_? or similar
(?: ...) is using a non-capturing group, also suitable for re.find
- The last
? will make the whole non-capturing group optional
Some comments to your approaches:
- You don't need to concatenate regex strings. The verbose flag is very helpful. You can even comment your parts of the regex.
- With
[\w|\w] you maybe want to express \w or \W. I don't know, it's just a guess. Then, it is easier to write . instead, matching everything on a single line.
- A non-greedy match
*? is not necessary if the part with the nextdate is optional.
- With
?:(...) you maybe wanted to express the non-capturing group. But the syntax is that you have to put the ?: as first element inside the parenthesis as you can see above in my example.
- Making something optional is done by
? at the end of an expression. If you apply this on a group like (...)?, it makes the whole group optional. If it is applied on a single character like a?, it would make the a optional. A single letter could also be expressed as a set of characters. [\w|\w]? will therefore also recognize only one character (without knowing exactly what you want to gain exactly from this expression).
regextag info page for guidance and answers to several beginner FAQs. It's unclear what you hope for[\w|\w]to match but it is exactly equivalent to[\w|], i.e. a character class which can match one character which is a "word" character (\w) or the literal|.