Trying to improve my regex skills, I wanted to learn about lookahead and lookbehind expressions. On my Archlinux system I tried the following:
a=ab;if [[ $a =~ [a-z](?=b) ]]; then echo "Y";else echo "N";fi
Which, as far as I understand it, should match and thus echo out a "Y", but doesn't.
echo ab |sed 's/[a-z](?=b)/x/'
...also doesn't seem to match.
grep
doesn't seem to lookaround either, but pcregrep
does. I also tried several attempts on quoteing and/or escaping the expressions, to no avail.
I'm a little confused, now. Could someone please clarify where lookaround, which doesn't seem that exotic judging from the number of mentions in tutorials, can actually be used? Or did I just mess up escaping my expressions?
Lookaround assertions aren't supported by basic or extended posix regular expressions which are available in bash or sed.
A good tool to test is GNU grep which supports the -P
option for perl compatible regular expressions. Like this:
grep --color=auto -P '[a-z](?=b)' <<< 'ab'
Even a greater resource are online regex testing tools like https://regex101.com/
You should distinguish between basic and extended Regular Expressions.
In basic regular expressions the meta-characters ?
, +
, {
, |
, (
, and )
lose their special meaning; They need to be escaped to get their "regex" meaning.
On the other hand, in the extended Regular Expressions, these characters get their "regex" meaning.
If you grep --help
, you'll get:
-E
,--extended-regexp
PATTERN is an extended regular expression (ERE)
Note that grep doesn't support look-arounds, it's supported in pcregrep.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With