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/