Posts

Showing posts with the label Regular Expression

Bash =~ Regex And Https://regex101.com/

Answer : \d is a nonstandard way for saying "any digit". I think it comes from Perl, and a lot of other languages and utilities support Perl-compatible REs (PCRE), too. (and e.g. GNU grep 2.27 in Debian stretch supports the similar \w for word characters even in normal mode.) Bash doesn't support \d , though, so you need to explicitly use [0-9] or [[:digit:]] . Same for the non-capturing group (?:..) , use just (..) instead. This should print match : temp="eu-west 140.243.64.99 " regexp="([0-9]{1,3}\.)+([0-9]{1,3})" [[ $temp =~ $regexp ]] && echo match (:...) and \d are perl or PCRE regular expression operators (like in GNU grep -P ). bash only supports extended regular expressions as in grep -E except that for regexps passed literally as in [[ text =~ regexp-here ]] as opposed to as the result of an unquoted expansion (as in [[ text =~ $var ]] or [[ test =~ $(printf '%s\n' 'reg...

Bash Regex Capture Group

Answer : It's a shame that you can't do global matching in bash. You can do this: global_rematch() { local s=$1 regex=$2 while [[ $s =~ $regex ]]; do echo "${BASH_REMATCH[1]}" s=${s#*"${BASH_REMATCH[1]}"} done } global_rematch "$mystring1" "$regex" 1BBBBBB 2AAAAAAA This works by chopping the matched prefix off the string so the next part can be matched. It destroys the string, but in the function it's a local variable, so who cares. I would actually use that function to populate an array: $ mapfile -t matches < <( global_rematch "$mystring1" "$regex" ) $ printf "%s\n" "${matches[@]}" 1BBBBBB 2AAAAAAA To get the second array value, you need to have a second set of parentheses in the regex: mystring1='<link rel="self" href="/api/clouds/1/instances/1BBBBBB"/> dsf <link rel="self" href="/api/cloud...

Any Non-whitespace Regular Expression

Answer : In non-GNU systems what follows explain why \S fail: The \S is part of a PCRE (Perl Compatible Regular Expressions). It is not part of the BRE (Basic Regular Expressions) or the ERE (Extended Regular Expressions) used in shells. The bash operator =~ inside double bracket test [[ use ERE. The only characters with special meaning in ERE (as opposed to any normal character) are .[\()*+?{|^$ . There are no S as special. You need to construct the regex from more basic elements: regex='^b[^[:space:]]+[a-z]$' Where the bracket expression [^[:space:]] is the equivalent to the \S PCRE expressions : The default \s characters are now HT (9), LF (10), VT (11), FF (12), CR (13), and space (32). The test would be: var='big' regex='^b[^[:space:]]+[a-z]$' [[ $var =~ $regex ]] && echo "$var" || echo 'none' However, the code above will match bißß for example. As the range [a-z] will include other char...