My problem is about a regex. Thanks to regexr, I managed to write up an expression that matches the interface name route linux command gave me.

But I can't catch it with:

route | grep -Eo "(?:((\w|\.|\*)+\s+)){7}([^ .]+[0-9]+)$"

Why is that?

  • The immediate issue I see is you are using regexr with the regex wrapped in /(?:((\w|\.|\*)+\s+)){7}([^ .]+[0-9]+)$/igm. And when you are using grep it is just (?:((\w|\.|\*)+\s+)){7}([^ .]+[0-9]+)$. Since you desire a multiline grep that just doesn’t exist; grep is strictly one line at a time. – Giacomo1968 Nov 3 '14 at 4:24
  • There are a bunch of different regex dialects. grep -E and JavaScript (regexr's dialect, from a quick look) don't use the exact same one, so you can only use the regexr output as a starting point. – jjlin Nov 3 '14 at 6:51

Different programs (and different versions of said program) might use different regex implementations. Try reducing or breaking down your regex part by part to know which feature is supported/is not supported.

For example, I need to remove the non-capturing group specifier ?: from your example so that my GNU grep can run as expected in Solaris 11.

Or you can try to replace -E with -P to use Perl regex engine which, in my case, make my GNU grep able to run your regex without further modifications.

route | grep -Po "(?:((\w|\.|\*)+\s+)){7}([^ .]+[0-9]+)$"

Note: GNU grep in Solaris 11 is located in /usr/gnu/bin/grep

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.