I'm working on a regex pattern that will match a pattern, or any subString of the pattern, occurring at the end of the string. While what I have below works and is fairly easy to understand, I'm sure there's gotta be a more elegant way to do this. I've looked into boundary matches and quantifiers but just don't see a great way to mash em up to get something like this. Any ideas?
regex = (_part_\d+$|_part_$|_part$|_par$|_pa$|_p$|_$)
aString_part_1 - match
aString_part_ - match
aString_part - match
aString_par - match
aString_pa - match
aString_p - match
aString_ - match
aString - no match
$, and I don't understand the pupose of the capture group. Another way, which I personally don't like as well, is_(?:p(?:a(?:r(?:t(?:_(?:\d)?)?)?)?))?$. Demo._(?:part_\d+|part_|part|par|pa|p)?$. Demo_from the front of the group. It will still be required, but not part of group 1. However since it is both fix and contained in the general match, you can leave it out. Modified my answer accordingly._, but expluding it will never work!(?:\d)?was wrong, since you missed the+, so should be(?:\d+)?, which can be shortened to\d*. --- 2) In the second example, the first two options can be merged, i.e.part_\d+|part_→part_\d*. --- In both cases,\d+→\d*simplifies the regex.