【发布时间】:2012-08-14 13:19:33
【问题描述】:
考虑这个文本文件:
TEST FILE : test #No match
#No match
Run grep "1133*" on this file #Match
#No match
This line contains the number 113. #Match
This line contains the number 13. #No match
This line contains the number 133. #No match
This line contains the number 1133. #Match
This line contains the number 113312. #Match
This line contains the number 1112. #No match
This line contains the number 113312312. #Match
This line contains no numbers at all. #No match
grep 命令如何评估1133* 正则表达式?
echo "Consider this text file:
TEST FILE : test #No match
#No match
Run grep \"1133*\" on this file #Match
#No match
This line contains the number 113. #Match
This line contains the number 13. #No match
This line contains the number 133. #No match
This line contains the number 1133. #Match
This line contains the number 113312. #Match
This line contains the number 1112. #No match
This line contains the number 113312312. #Match
This line contains no numbers at all. #No match" | grep "1133*"
输出:
Run grep "1133*" on this file #Match
This line contains the number 113. #Match
This line contains the number 1133. #Match
This line contains the number 113312. #Match
This line contains the number 113312312. #Match
为什么包含113 的行是肯定的?
正则表达式1133* 是否意味着除了
找出所有包含单词1133+anything else的行?
此示例可在 tldp regexp 文档页面上找到。
【问题讨论】:
-
1133* 匹配 113 或 1133 或 11333 等,或“出现在 * 之前的任何元素的零次或多次出现”。
-
您将从学习正则表达式的基础知识中获得巨大的好处(例如here)。
-
Lucian,你能分享你的 grep 命令吗?
-
@AlikElzin-kilaka 我很难记住 8 年前发生了什么,但为了完整起见,我更新了我的问题以包括源代码以及正则表达式本身。