Let's say I've got a specific file looking like that:
#tata toto
tata titi
tata tutu titi
#tata titi
tata toto #ZZZ
tata toto #ZZZ
#tata toto #ZZZ
tata titi #YYY
#tata titi #YYY
tata titi toto
Ans I want to match every line:
- starting with tata
- capture if toto is present or not
For example if
tata titi => \1=tata \2=" titi" \3=null \4=null
tata titi toto => \1=tata, \2=" titi ", \3=toto, \4=null
tata toto tutu => \1=tata, \2=" ", \3=toto, \4=" tutu"
I've tryed this regex:
^(tata)(.*)(toto)?(.*)
But the .*
is capturing more than expected. So toto is never captured.
How would you do that?
To gives more context, I want to parse an /etc/hosts: if I found a specific IP (here tata), but this line does not contains an hostname alias (here toto), we add it, conserving all hostname and hostname alias already defined, and the comment.
Thanks, Raoul