Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex: How to match pattern only once?

Tags:

regex

sed

I'm trying to extract data from a .config file (generated by using kconfig). The default format is:

SYMBOL=y (in case of a bool)
SYMBOL="str" (in case of a string)

I did managed to get it working with the following regex:

sed -e '/^#/d;s/\(.+\)=\(.+\)/def \1 "\1"\n/g' configfile > formattedfile

It is working for any case except for this one:

SYMBOL="http://my.domain/toast?id=150"

As a result, I have in my output file:

def SYMBOL="http://my.domain/toast?id "SYMBOL="http://my.domain/toast?id="

Because the pattern XXX=XXX appears twice in this line. How can I avoid this please ?

Regards,

like image 431
Lazao Avatar asked Nov 20 '25 13:11

Lazao


1 Answers

You need to escape the + symbol and also turn the first .+ to [^=]\+ because .+ is greedy and matches upto the last = symbol.

$ sed -e '/^#/d;s/\([^=]\+\)=\(.\+\)/def \1 "\1"\n/g' file
def SYMBOL "SYMBOL"

def SYMBOL "SYMBOL"

def SYMBOL "SYMBOL"
like image 73
Avinash Raj Avatar answered Nov 22 '25 04:11

Avinash Raj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!