正则表达式中带有“ \ n”的grep行为

问题描述 投票:0回答:1

有人可以解释这种行为吗?

我在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

案例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]](brep中的grep)

[test@th3]$ grep -E '^(b8|a8|8d){1,1}$' <( printf 'a8\n'; )
a8
[test@th3]$

# it matches the 'a8'

this问题中,有人回答说$代表REGEX中的行尾,因此在情况3中grep与'\n'匹配,但在其他情况下为什么不匹配?

请忽略我在Java,JavaScript情况下的评论样式,因为我无法使用Java,JavaScript标记此问题。

有人可以解释这种行为吗?我在3种不同情况下将字符串与模式匹配。情况1:在Java模式中pattern = Pattern.compile(“ ^(b8 | a8 | 8d){1,1} $”)); #A虚拟模式匹配器匹配器...

grep将从每一行中删除尾随的换行符,就像从文件中读取一样。假定其输入为POSIX文本文件,而不是任意字节流,因此每行输入均不包含换行符。

regex bash grep pcre
1个回答
2
投票

grep将从每一行中删除尾随的换行符,就像从文件中读取一样。假定其输入为POSIX文本文件,而不是任意字节流,因此每行输入均不包含换行符。

© www.soinside.com 2019 - 2024. All rights reserved.