grep行为,带有\";正则表达式
有人能解释一下这种行为吗 我在3种不同的情况下用模式匹配字符串 案例1:在Java中grep行为,带有\";正则表达式,grep,Grep,有人能解释一下这种行为吗 我在3种不同的情况下用模式匹配字符串 案例1:在Java中 Pattern pattern = Pattern.compile("^(b8|a8|8d){1,1}$"); #A dummy pattern Matcher matcher = pattern.matcher("a8\n"); #A dummy string boolean result = matcher.matches(); #result is false, which is expected
Pattern pattern = Pattern.compile("^(b8|a8|8d){1,1}$"); #A dummy pattern
Matcher matcher = pattern.matcher("a8\n"); #A dummy string
boolean result = matcher.matches();
#result is false, which is expected
案例2:在JavaScript中
var str = "a8\n";
var patt = /^(b8|a8|8d){1,1}$/g;
var result = str.match(patt);
#result prints nothing, again it's expected
然而
案例3:(bash中的grep)
[test@th3]$grep-E'^(b8 | a8 | 8d){1,1}$'grep
从每一行中剥离尾随的换行符,就像它从文件中读取一样。它的输入被假定为一个POSIX文本文件,而不是一个任意字节流,因此每一个输入行都是无换行的。Trygrep-E'^(b8 | a8 | 8d){1,1}$'“为什么人们写{1,1}
或{1}
”是新希尔伯特的问题。@Wiktor Stribiżew,您能解释一下为什么删除了regex
标记吗?虽然问题意图与regex
没有直接关系,但在这里是必需的。@Casimir et Hippolyte,是的,你是对的,我是regex的新手,在这种情况下,{1,1}
不需要替换。问题与regex无关,只与grep有关。
[test@th3]$ grep -E '^(b8|a8|8d){1,1}$' <( printf 'a8\n'; )
a8
[test@th3]$
# it matches the 'a8'