regex - domain name regular expression not matching -
i have shell script @ /www/cgi-bin/test can access on network @ http://192.168.1.1/cgi-bin/test
.
i attempting parse query string, should d=domain.com
, , validate against regular expression:
#!/bin/sh echo "content-type: text/html" echo "" domain=${query_string#d=} if [[ ! $domain =~ [a-za-z0-9-]+(\.[a-za-z0-9-]+)*(\.[a-za-z]{2,}) ]]; exit fi echo "validation success!"
when didn't work, tried using regex stole here:
if [[ ! $domain =~ \ ^(([a-za-z](-?[a-za-z0-9])*)\.)*[a-za-z](-?[a-za-z0-9])+\.[a-za-z]{2,}$ \ ]]; exit fi
i can't regex match either. in both cases, tried escaping curly braces (\{2,\}
) according advanced bash-scripting guide, didn't make difference.
in case it's relevant, platform i'm on openwrt 12.09.
edit: realized shell script might not support bash's [[ ... =~ ... ]]
syntax. unfortunately openwrt doesn't ship bash.
if don't have bash
, and/or cannot replace shebang #!/bin/bash
, [[
expression might not work, or pattern substitution ${query_string#pattern}
might not work.
in case, can use awk
hit 2 birds 1 stone:
if ! echo $query_string | awk '$0 !~ /^d=[a-za-z0-9-]+(\.[a-za-z0-9-]+)*(\.[a-za-z]{2,})$/ {exit 1}'; exit 1 fi
or, if pattern substitution works, , regex doesn't can use expr
instead of awk
:
domain=${query_string#d=} if ! expr $domain : '[a-za-z0-9-]\{1,\}\(\.[a-za-z0-9-]\{1,\}\)*\(\.[a-za-z]\{2,\}\)$' >/dev/null; exit 1 fi
in both cases, used bit more strict pattern d=
in query_string
. in both cases, careful end pattern $
, otherwise things domain.com-
pass.
Comments
Post a Comment