Some question when using regular expression , ask for help!
Please see the following code,
Code:
[saturn@saturn-pc new]$ [[ "aab" =~ ab ]] && echo "ok" || echo "error"; Code:
[saturn@saturn-pc new]$ [[ "aab" =~ a*b ]] && echo "ok" || echo "error" Thanks! |
Quote:
Quote:
|
In a regular expression '*' does not mean match any character zero or more occurences.
This is confusing, as in filename matching it does. If you tried the ls command, ls *ab would match aab. What matches any characted in a regular expression is '.' (period). So matching any character zero or more times would be '.*'. To find it at the start of a line, use '^.*'. jlinkels |
Quote:
Code:
$ [[ "aab" =~ \\bab\\b ]] && echo "ok" || echo "error" Anyway this behaviour works only until bash version 3.1. To do the same in bash 3.2 and newer, you have to set the option compat31: Code:
shopt -s compat31 |
Thanks syg00
I thinks ab is only a fixed string, it does not contain any metacharacter such as . or * or ?, so I am very puzzled. Code:
[saturn@saturn-pc new]$ [[ "aab" =~ ab ]] && echo "ok" || echo "error" |
Regular expression is not the same as shell globbing...if you want to match "ab", then use a fixed string...in the shell, you can just do a simple case/esac without using regular expression
Code:
case "aab" in |
Quote:
--- rod. |
Thanks all
|
All times are GMT -5. The time now is 10:41 PM. |