Regex: How to capture optional group in middle of text?

前端 未结 2 609
忘了有多久
忘了有多久 2021-01-25 14:18

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

相关标签:
2条回答
  • 2021-01-25 14:41

    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)

    0 讨论(0)
  • 2021-01-25 14:51

    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.

    0 讨论(0)
提交回复
热议问题