regex - capture optional substring -


i'm trying match these lines:

x=1 b c y=5 bb aa x=2 aa ccc d x=3 ccc y=9 d ee ff 

the 'y=x' substring optional. may present in line or not. want capture x=x in group 1, , y=x in optional group 2.

i'm using regular expression:

^(x=[0-9]+?).+?(y=[0-9]+?)?.+?$ 

the whole line matched, i'm capturing first group. second group never captured if optional substring exists. problem between x= , y= lot of different words (with '=' character too).

any ideas how capture optional group 2?

actual example: http://regex101.com/r/rd4so4/1

updated

here's more efficient , cleaner option (that doesn't require alternation):

^(x=[0-9]+)(?:.+?(y=[0-9]+))?.+?$ 

demo

note removed lazy repetition [0-9] since shouldn't have issues matching many characters anyways.


explanation

the original expression wasn't working because regular expression match left right (and since using lazy repetition, assume understand concept regex "greedy"). though middle .+? lazy, y=[0-9]+ optional continue match way trailing/required .+?$. may not 1 expect, since there lazy repetition..but why stop optional segment when can backtrack , try match again.

mine worked because made middle .+? exist if y=[0-9]+ existed. y=[0-9]+ checked for, if not found okay since whole group still optional. still can end trailing .+?$.


original

i'm racking brain more concise answer, used alternation make regex engine check y=[0-9]+? before matching everything:

^(x=[0-9]+)(?:.+?(y=[0-9]+).+?|.+?)$ 

demo


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -