简体   繁体   English

两个正则表达式字符\\ b和\\有什么区别

[英]What's the difference the two regular expression characters \b and \<

In the script I'm reading about regular expressions it says: 在脚本中,我正在阅读正则表达式,它说:

  • '\\b' hits the front border of the word '\\b'击中了单词的前边框
  • '\\<' hits the start of the word '\\<'点击了这个词的开头

So what's the difference in using the following 那么使用以下内容有什么不同

  • \\b over \\< \\b over \\<
  • \\b over \\> \\b over \\>

My man grep tells me about \\b : 我的man grep告诉我关于\\b

The symbols \\< and \\> respectively match the empty string at the beginning and end of a word. 符号\\<\\>分别匹配单词开头和结尾的空字符串。 The symbol \\b matches the empty string at the edge of a word, [...] 符号\\b匹配单词边缘的空字符串,[...]

So \\bfoo\\b would match wherever \\<foo\\> would match. 所以\\bfoo\\b会匹配\\<foo\\>匹配的地方。

On the other hand: There are so many regexp variants, that it is hard to tell what yours is doing with \\b . 另一方面:有很多正则表达式变体,很难说你用它做什么\\b

\\b is like \\< and \\> combined: \\b就像\\<\\>组合:

  • \\< match at beginning of word, \\<在单词开头匹配,
  • \\> match at end of word, \\>在单词结尾处匹配,
  • \\b match at the begining OR end of word, \\b在单词的开头或结尾处匹配,
  • \\B matches except at the beginning or end of a word. \\B匹配除了单词的开头或结尾。

Your source appears to be wrong, or at least incomplete. 您的来源似乎是错误的,或者至少是不完整的。 \\b matches any border, not just the front one. \\b匹配任何边框,而不仅仅是前边框。 Quote man grep : 引用man grep

The symbols \\< and \\> respectively match the empty string at the beginning and end of a word. 符号\\<\\>分别匹配单词开头和结尾的空字符串。 The symbol \\b matches the empty string at the edge of a word 符号\\b匹配单词边缘的空字符串

  • grep 's \\b is equivalent to grep 's \\(\\<\\|\\>\\) grep 's \\b相当于grep\\(\\<\\|\\>\\)

In case you are familiar with Perl regular expressions, 如果您熟悉Perl正则表达式,

  • grep 's \\< is equivalent to Perl's (?<!\\w)(?=\\w) grep\\<等同于Perl (?<!\\w)(?=\\w)
  • grep 's \\> is equivalent to Perl's (?<=\\w)(?!\\w) grep\\>相当于Perl的(?<=\\w)(?!\\w)
  • grep 's \\b is equivalent to Perl's \\b grep\\b等同于Perl的\\b
  • grep 's \\b is equivalent to Perl's (?:(?<!\\w)(?=\\w)|(?<=\\w)(?!\\w)) grep 's \\b相当于Perl (?:(?<!\\w)(?=\\w)|(?<=\\w)(?!\\w))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM