August 31st, 2013, 05:14 AM
Optional capture group
I want to match test.nl.example.org and then my first match group is test.nl. Therefor is use this regex:
But now I want if there is any options given (with opt-) I want that options in the second capture group:
So the $2 of test.nl.opt-ob.example.org is: ob
Unfortunately the regex doesn't match test.nl.example.org can I make from the second group a optional group?
I've tried this:
but then the content of $2 is empty.
I hope that someone can help me with this.
August 31st, 2013, 07:01 AM
+ is greedy by default, meaning it'll match as much as it possibly can before the regular expression engine will backtrack and try to match the rest. Your opt-* thing is optional so it'll just skip that entirely.
Use the ungreedy version +?, which will match as little as possible, with your latest attempt.
Comments on this post
August 31st, 2013, 07:35 AM
Thanks it's working, this is my final regex:
Originally Posted by requinix