I\'m struggling with using regex to capture some optional text - it\'s in the middle of some filenames, but not all. The big problem appears to be that my optional group is not
Try putting the .*?(foo)
parts in parentheses like (.*?(foo))
so that the ?
operator will take the .*?
parts into consideration.
Corrected syntax (.*?).\(?(\d{4})(.*?(color))?.*?(shape)(.*?(color))?(.*?(version))?.*
(example)
I'm not particularly familiar with the format you are trying to use, but I believe I came up with an example of something that tackles the problem you have.
http://regexr.com/3cs8g
/name ((\([0-9]+\))|([0-9]+)) ((color shape)|(shape( color)?))( version)?/ig
What your describing is an optional clause that can appear in one of two spots. So, you make an or statement to test for each possible spot.