如何匹配“ABC-123”但不是“XABC-123”在正则表达式中

发布于 2024-11-08 18:19:55 字数 605 浏览 0 评论 0原文

我有这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

你如我软肋 2024-11-15 18:19:55

您似乎正在寻找 \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]+.

送舟行 2024-11-15 18:19:55

应该这样做:

^(ABC-[0-9]+)

这样您就可以告诉您希望该行以您的正则表达式开头。

This should do :

^(ABC-[0-9]+)

This way you're telling you want the line to start with your regexp.

时光是把杀猪刀 2024-11-15 18:19:55

如果 \b 不适合您,您是否尝试过 ((^| )ABC-[0-9]+)

If \b doesn't work for you, have you tried ((^| )ABC-[0-9]+)?

薯片软お妹 2024-11-15 18:19:55

尝试以下操作:

echo "XABC-123" | egrep -is "(\bABC-[0-9]+)" && echo "ok"

有几个解决方案建议使用 ^ (以...开头),但是,如果您正在查看可能想要捕获的“ABC-123”,它们将会失败。单词边界可能是您想要的,除非您正在寻找开头...

以下是一些示例输出:

tim@Ikura ~
$ echo " ABC-123" | egrep -is "(\bABC-[0-9]+)" && echo "ok"
 ABC-123
ok

tim@Ikura ~
$ echo "ABC-123" | egrep -is "(\bABC-[0-9]+)" && echo "ok"
ABC-123
ok

tim@Ikura ~
$ echo "XABC-123" | egrep -is "(\bABC-[0-9]+)" && echo "ok"

tim@Ikura ~
$

更新: Solaris issues...“搜索单词并不像它那么简单at 首先出现。您可以在字母前后添加空格并使用以下正则表达式:“ the ”,但是这不会匹配 the 开头或结尾的单词。并且它不匹配单词后面有标点符号的情况,

有一个简单的解决方案,字符“\<”和“>”类似于“^”和“$”锚点。因为它们不占据字符的位置。它们将表达式“锚定”到仅在单词边界上时才匹配。搜索单词“the”的模式将是“\<[tT]”。 he>"。“t”之前的字符必须是换行符,或者除字母、数字或下划线之外的任何字符。“e”之后的字符也必须是除数字、字母或下划线之外的字符。下划线或者它可能是行结束符。”

tim@Ikura ~
$ echo "XABC-123" | egrep -is "(\<ABC-[0-9]+\>)" && echo "ok"

tim@Ikura ~
$ echo " ABC-123" | egrep -is "(\<ABC-[0-9]+\>)" && echo "ok"
 ABC-123
ok

Try the following:

echo "XABC-123" | egrep -is "(\bABC-[0-9]+)" && echo "ok"

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:

tim@Ikura ~
$ echo " ABC-123" | egrep -is "(\bABC-[0-9]+)" && echo "ok"
 ABC-123
ok

tim@Ikura ~
$ echo "ABC-123" | egrep -is "(\bABC-[0-9]+)" && echo "ok"
ABC-123
ok

tim@Ikura ~
$ echo "XABC-123" | egrep -is "(\bABC-[0-9]+)" && echo "ok"

tim@Ikura ~
$

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."

tim@Ikura ~
$ echo "XABC-123" | egrep -is "(\<ABC-[0-9]+\>)" && echo "ok"

tim@Ikura ~
$ echo " ABC-123" | egrep -is "(\<ABC-[0-9]+\>)" && echo "ok"
 ABC-123
ok
这样的小城市 2024-11-15 18:19:55
echo "XABC-123" | egrep -is "^ABC-[0-9]+" && echo "ok"

编辑:当前面没有字母时接受 ABC

echo "XABC-123" | egrep -is "(^|[^A-Z])ABC-[0-9]+" && echo "ok"
echo "XABC-123" | egrep -is "^ABC-[0-9]+" && echo "ok"

EDIT: To accept ABC when anything but a letter precedes it:

echo "XABC-123" | egrep -is "(^|[^A-Z])ABC-[0-9]+" && echo "ok"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文