Showing posts with label regular expressions. Show all posts
Showing posts with label regular expressions. Show all posts

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(.*)




Thursday, August 14, 2008

Regular expressions: Remove last appearance of some element

If you want to match A, B and C but don't want C to be the last element you can do it using this regular expression:



[ABC]+(?<!C)





Note that [ABC]+ can be mutch more complex then this is.

Tuesday, June 24, 2008

Convert std::string to std::wstring

// std::string -> std::wstring
std::string s("string");
std::wstring ws;
ws.assign(s.begin(), s.end());

// std::wstring -> std::string
std::wstring ws(L"wstring");
std::string s;
s.assign(ws.begin(), ws.end());