Search code examples
regex-lookaroundsregex-greedy

linux grep with perl regex greedy not work


my perl version on linux server is :

This is perl 5, version 16, subversion 3 (v5.16.3) built for x86_64-linux-thread-multi

I have a test as below.

echo "mac:abcdefg1234" | grep -Po "(?<=mac:).*(?=\d+)"

The result is abcdefg123.

But the greedy match does not work.The result I want is abcdefg. How can I get the content between "mac:" and "digital" (as many as is allowed)


Solution

  • (?<=mac:)[^\d]*(?=\d+) thats the content beetween.

    [^\d]* means all not digital with length >=0. Typing a ^ after [ negates the character class. The result is that the character class matches any character that is not in the character class. It also match (invisible) line break characters.