Friday, August 03, 2012

Lookbehind assertion is not fixed length

I am using grep for extracting string from file.
If my regular expression looks like:


grep -P -o -z '(?<=^ManagementCidr\s*=\s*).*'

I will get an error "Lookbehind assertion is not fixed length". Problem is in \s*. (?<=) works only if match if fixed size.

Regular expression

(?<=^ManagementCidr\s*=\s*).*
is OK in C# (you can test it using this great tool).


Fortunately, after hours of googling I found that if you put in regular expressing for grep \K it will reset matched text at this point. So grep command that do what I want will be:


grep -P -o -z '^ManagementCidr\s*=\s*\K(.*)




4 comments:

Unknown said...

Thank you!

You saved me a lot of trouble and time :)

jjwilly said...

Thanks for posting this!

Charles said...

Thanks man!

Matzerich said...

Never heard of \K before.
Thank you for this!