regex - 如何修复正则表达式以匹配整个单词,而不是子字符串?

标签 regex linux bash ubuntu

<分区>

我还没有找到修复这个正则表达式的任何成功: B..y

我目前正在搜索一个文本文件,其输出如下: 婴儿 巴比伦 婴儿乐园 电子宝贝

我应该在表达式中更改什么以仅输出“Baby”并排除其他三个?

编辑:如果我有另一个条目 - 'Blay' 怎么办?我需要得到“Baby”和“Blay”。

最佳答案

正则表达式:

\bBaby\b

测试here .


要同时找到“Baby”和“Blay”,您需要将正则表达式更新为:

\b(Baby|Blay)\b

测试here .


解释:

来自 here关于\b:

The metacharacter \b is an anchor like the caret and the dollar sign. It matches at a position that is called a “word boundary”. This match is zero-length.

There are three different positions that qualify as word boundaries:

  • Before the first character in the string, if the first character is a word character.
  • After the last character in the string, if the last character is a word character.
  • Between two characters in the string, where one is a word character and the other is not a word character.

Simply put: \b allows you to perform a “whole words only” search using a regular expression in the form of \bword\b. A “word character” is a character that can be used to form words. All characters that are not “word characters” are “non-word characters”.

来自 here关于 (Baby|Blay) :

If you want to search for the literal text cat or dog, separate both options with a vertical bar or pipe symbol: cat|dog. If you want more options, simply expand the list: cat|dog|mouse|fish.

The alternation operator has the lowest precedence of all regex operators. That is, it tells the regex engine to match either everything to the left of the vertical bar, or everything to the right of the vertical bar. If you want to limit the reach of the alternation, you need to use parentheses for grouping. If we want to improve the first example to match whole words only, we would need to use \b(cat|dog)\b. This tells the regex engine to find a word boundary, then either cat or dog, and then another word boundary. If we had omitted the parentheses then the regex engine would have searched for a word boundary followed by cat, or, dog followed by a word boundary.

关于regex - 如何修复正则表达式以匹配整个单词,而不是子字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59733121/

相关文章:

php - FastCGI PHP7.2 在 Windows 上总是超时 Nginx Bash ubuntu

linux - 运行 cron 产生的结果与运行它的命令不同

javascript - 如何使用正则表达式从 url 中删除参数

linux - 当有两个 gpu 时,如何设置 Torch 只使用一个 gpu?

bash - 如何使用 sed 在 mac 终端中查找和替换字符串?

linux - 关于 Lubuntu 16.04 (i386) ICOP 板中的 QT 创建者

c++ - 处理 GLIBC 版本

javascript - 正则表达式匹配非ASCII字符?

javascript string.replace 不工作

java - 正则表达式和前瞻 : java