1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
# Written by Aleksey Cheusov <vle@gmx.net>, public domain
#
# This awk module is a part of RunAWK distribution,
# http://sourceforge.net/projects/runawk
#
############################################################
# =head2 str2regexp.awk
#
# =over 2
#
# =item I<str2regex(STRING)>
#
# returns a regular expression that matches given STRING
#
# =back
#
# For example:
# print str2regexp("all special symbols: ^$(){}[].*+?|\\")
# -| all special symbols: [^][$][(][)][{][}][[]\][.][*][+][?][|]\\
#
#use "alt_assert.awk"
function __runawk_mawk_bug_test (tmp){
# returns true if buggy MAWK
tmp = "\\\\"
gsub(/\\/, "\\\\", tmp)
return (tmp != "\\\\\\\\")
}
BEGIN {
__buggy_mawk = __runawk_mawk_bug_test()
}
function str2regexp (s){
gsub(/\[/, "---runawk-open-sq-bracket---", s)
gsub(/\]/, "---runawk-close-sq-bracket---", s)
gsub(/[?{}|()*+.$]/, "[&]", s)
gsub(/\^/, "[\\^]", s)
if (s ~ /\\/){
if (!__buggy_mawk){
# normal AWK
gsub(/\\/, "\\\\", s)
}else{
# MAWK /-(
gsub(/\\/, "\\\\\\\\\\", s)
}
}
gsub(/---runawk-open-sq-bracket---/, "\\[", s)
gsub(/---runawk-close-sq-bracket---/, "\\]", s)
return s
}
|