如何匹配“ABC-123”但不是“XABC-123”在正则表达式中
我有这个 egrep 搜索:
egrep -is "(ABC-[0-9]+)"
它与字符串中任何位置的 ABC-123 匹配。
我希望它忽略 XABC-456 或 YABC-789。
换句话说,这些示例应该输出“ok”:
echo "ABC-123" | egrep -is "(ABC-[0-9]+)" && echo "ok"
echo "test ABC-123" | egrep -is "(ABC-[0-9]+)" && echo "ok"
但这不应该:
echo "XABC-123" | egrep -is "(<fill in>ABC-[0-9]+)" && echo "ok"
我尝试了这个但没有任何运气(无输出):(
echo "ABC-123" | egrep -is "(\bABC-[0-9]+)" && echo "ok"
我正在运行 Solaris 10)
我该怎么做?
I have this egrep search:
egrep -is "(ABC-[0-9]+)"
which matches ABC-123 anywhere in a string.
I'd like it to ignore XABC-456 or YABC-789.
In other words, those examples should output "ok":
echo "ABC-123" | egrep -is "(ABC-[0-9]+)" && echo "ok"
echo "test ABC-123" | egrep -is "(ABC-[0-9]+)" && echo "ok"
But this shouldn't:
echo "XABC-123" | egrep -is "(<fill in>ABC-[0-9]+)" && echo "ok"
I tried this without any luck (no output):
echo "ABC-123" | egrep -is "(\bABC-[0-9]+)" && echo "ok"
(I'm running Solaris 10)
How can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您似乎正在寻找
\bABC-[0-9]+
- 字边界。另一种选择是使用否定lookbedind,它可以让您更好地控制什么可以什么不可以位于比赛之前:
(?。
It look like you're looking for
\bABC-[0-9]+
- Word Boundaries.Another option is to use a negetive lookbedind, whci gives you more control over what can and cannot be before the match:
(?<![a-z])ABC-[0-9]+
.应该这样做:
这样您就可以告诉您希望该行以您的正则表达式开头。
This should do :
This way you're telling you want the line to start with your regexp.
如果
\b
不适合您,您是否尝试过((^| )ABC-[0-9]+)
?If
\b
doesn't work for you, have you tried((^| )ABC-[0-9]+)
?尝试以下操作:
有几个解决方案建议使用 ^ (以...开头),但是,如果您正在查看可能想要捕获的“ABC-123”,它们将会失败。单词边界可能是您想要的,除非您正在寻找开头...
以下是一些示例输出:
更新: Solaris issues...“搜索单词并不像它那么简单at 首先出现。您可以在字母前后添加空格并使用以下正则表达式:“ the ”,但是这不会匹配 the 开头或结尾的单词。并且它不匹配单词后面有标点符号的情况,
有一个简单的解决方案,字符“\<”和“>”类似于“^”和“$”锚点。因为它们不占据字符的位置。它们将表达式“锚定”到仅在单词边界上时才匹配。搜索单词“the”的模式将是“\<[tT]”。 he>"。“t”之前的字符必须是换行符,或者除字母、数字或下划线之外的任何字符。“e”之后的字符也必须是除数字、字母或下划线之外的字符。下划线或者它可能是行结束符。”
Try the following:
There are a couple solutions that propose using ^ (starts with...) however, they will fail if you are looking at " ABC-123" which you might want to catch. Word boundaries is probably what you want, unless you are looking for starts with...
Here's some sample output:
Update: Solaris issues... "Searching for a word isn't quite as simple as it at first appears. The string "the" will match the word "other". You can put spaces before and after the letters and use this regular expression: " the ". However, this does not match words at the beginning or end of the line. And it does not match the case where there is a punctuation mark after the word.
There is an easy solution. The characters "\<" and ">" are similar to the "^" and "$" anchors, as they don't occupy a position of a character. They do "anchor" the expression between to only match if it is on a word boundary. The pattern to search for the word "the" would be "\<[tT]he>". The character before the "t" must be either a new line character, or anything except a letter, number, or underscore. The character after the "e" must also be a character other than a number, letter, or underscore or it could be the end of line character."
编辑:当前面没有字母时接受
ABC
:EDIT: To accept
ABC
when anything but a letter precedes it: