pcre
1 #!/usr/bin/awk -f
2 function expand(seq)
3 {
4 return seq == "\\b" ? "(^|[^_[:alnum:]]|$)" : \
5 seq == "\\d" ? "[[:digit:]]" : \
6 seq == "\\D" ? "[^[:digit:]]" : \
7 seq == "\\s" ? "[[:space:]]" : \
8 seq == "\\S" ? "[^[:space:]]" : \
9 seq == "\\w" ? "[[:alnum:]_]" : \
10 seq == "\\W" ? "[^[:alnum:]_]" : \
11 seq
12 }
13
14 function pcre(re, head, repl, tail, rstr)
15 {
16 tail = re
17 while (match(tail, "\\\\[bdDsSwW]"))
18 {
19 head = substr(tail, 1, RSTART - 1) # text before match
20 repl = substr(tail, RSTART, RLENGTH) # match to replace
21 tail = substr(tail, RSTART + RLENGTH) # text after match
22 if ((match(head, /\\+$/) ? RLENGTH + 1 : 1) % 2 == 1)
23 repl = expand(repl)
24 rstr = rstr head repl
25 }
26 return rstr tail
27 }
28
29 # Test code for processing sample regex from stdin or file argument
30 { print $0 " -> " pcre($0) }Last updated