比较来自世界各地的卖家的域名和 IT 服务价格

Grep 没有显示结果,在线测试仪 regex 表明

我在行为中缺乏经验 grep. 我有一堆文件 XML, 包含此类行:


<identifier type="abc">abc:def.ghi/g1234.ab012345</identifier>
<identifier type="abc">abc:def.ghi/g5678m.ab678901</identifier>


我想在倾斜功能后获得一部分标识符并构建 regex, 使用
http://regexpal.com/
:


[a-z]\d{4}[a-z]*\.[a-z]*\d*


他突出了我想要的一切。 理想的。 现在我跑了 grep 在同一文件中,我没有得到任何结果。 而且,正如我所说,我真的不知道 grep, 因此,我尝试了所有各种组合。


grep [a-z]\d{4}[a-z]*\.[a-z]*\d* test.xml
grep "[a-z]\d{4}[a-z]*\.[a-z]*\d*" test.xml
egrep "[a-z]\d{4}[a-z]*\.[a-z]*\d*" test.xml
grep '[a-z]\d{4}[a-z]*\.[a-z]*\d*' test.xml
grep -E '[a-z]\d{4}[a-z]*\.[a-z]*\d*' test.xml


我究竟做错了什么?
已邀请:

三叔

赞同来自:

你的 regex 不对应于输入数据。 让我们搞清楚:


[a-z]

spichek.
g



\d{4}

spichek.
1234



[a-z]*

S.不一致
.


另外,我相信
grep

和家人不喜欢语法
\d

. 尝试
[0-9]

或者
[:digit:]


最后,在使用正则表达式时,更喜欢
egrep


grep

. 我不记得确切的细节,但是
egrep

支持更多运营商 regex. 此外,在许多贝壳中 /包括 bash 在 OS X, 正如您已经提到的那样,使用单引号而不是双引号,否则
*

将扩展 shell 之前在当前目录中的文件列表之前 grep 看到了 /和其他Metasimvols. shell 也将扩大/. Bash 什么都没有触摸单引号。

帅驴

赞同来自:

grep

不支持
\d

默认。 匹配数字,使用
[0-9]

或解决 Perl 兼容的正则表达式:


$ grep -P "[a-z]\d{4}[a-z]*\.[a-z]*\d*" test.xml


或者:


$ egrep "[a-z][0-9]{4}[a-z]*\.[a-z]*[0-9]*" test.xml

冰洋

赞同来自:

grep 用途 "basic" 常用表达 : /曝光来自人文页 /


Basic vs Extended Regular Expressions
In basic regular expressions the meta-characters ?, +, {, |, /, and / lose their
special meaning; instead use the backslashed versions \?, \+, \{, \|, \/, and
\/.

Traditional egrep did not support the { meta-character, and some egrep
implementations support \{ instead, so portable scripts should avoid { in
grep -E patterns and should use [{] to match a literal {.

GNU grep -E attempts to support traditional usage by assuming that { is not
special if it would be the start of an invalid interval specification. For
example, the command grep -E '{1' searches for the two-character string {1
instead of reporting a syntax error in the regular expression. POSIX.2 allows
this behavior as an extension, but portable scripts should avoid it.


另外,取决于哪个 shell 您在“*”符号中执行,可以扩展。

江南孤鹜

赞同来自:

您可以使用以下命令:


$ cat file
<identifier type="abc">abc:def.ghi/g1234.ab012345</identifier>

# Use -P option to enable Perl style regex \d.
$ grep -P '[a-z]\d{4}[a-z]*\.[a-z]*\d*' file
<identifier type="abc">abc:def.ghi/g1234.ab012345</identifier>

# to get only the part of the input that matches use -o option:
$ grep -P -o '[a-z]\d{4}[a-z]*\.[a-z]*\d*' file
g1234.ab012345

# You can use [0-9] inplace of \d and use -E option.
$ grep -E -o '[a-z][0-9]{4}[a-z]*\.[a-z]*[0-9]*' file
g1234.ab012345
$

小姐请别说爱

赞同来自:

试试吧:

[a-z]\d{5}[.][a-z]{2}\d{6}

二哥

赞同来自:

尝试这个表达式 grep:


[a-z]\d{4}[a-z]*\.[a-z]*\d*

快网

赞同来自:

首先,不要使用正则表达式进行句法分析 xml/html. 看到这个经典帖子
https://coderoad.ru/1732348/

要回复问题请先登录注册