0%

Regex

grep regex replace

perl-regex lookaround

https://learnbyexample.github.io/learn_gnugrep_ripgrep/perl-compatible-regular-expressions.html

Lookarounds helps to create custom anchors and add conditions to a pattern. These assertions are also known as zero-width patterns because they add restrictions similar to anchors and are not part of matched portions (especially helpful with -o option). These can also be used to negate a grouping similar to negated character sets. Lookaround assertions can be added to a pattern in two ways — lookbehind and lookahead. Syntax wise, these two ways are differentiated by adding a < for the lookbehind version. The assertion can be negative (syntax !) or positive (syntax =).

Syntax Lookaround type
(?!pattern) Negative lookahead
(?<!pattern) Negative lookbehind
(?=pattern) Positive lookahead
(?<=pattern) Positive lookbehind
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ # extract whole words only if NOT preceded by : or -
$ # can also use '(?<![:-])\b\w++'
$ echo ':cart<apple-rest;tea' | grep -oP '(?<![:-])\b\w+\b'
apple
tea

$ # note that end of string satisfies the given assertion
$ # 'bazbiz' has two matches as the assertion doesn't consume characters
$ echo 'boz42 bezt5 bazbiz' | grep -ioP 'b.z(?!\d)'
bez
baz
biz

$ # extract digits only if it is followed by ,
$ # note that end of string doesn't qualify as this is positive assertion
$ echo '42 foo-5, baz3; x-83, y-20: f12' | grep -oP '\d+(?=,)'
5
83
$ # extract digits only if it is preceded by - and not followed by ,
$ # note that this would give different results for greedy quantifier
$ echo '42 foo-5, baz3; x-83, y-20: f12' | grep -oP '(?<=-)\d++(?!,)'
20

grep regex match with only print matching

1
2
# print all codeblocks child-project path
cat xxx.workspace |grep -P '(?<=Project filename=")[^"]+' -o