简体   繁体   中英

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
  • '\\<' hits the start of the word

So what's the difference in using the following

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

My man grep tells me about \\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, [...]

So \\bfoo\\b would match wherever \\<foo\\> would match.

On the other hand: There are so many regexp variants, that it is hard to tell what yours is doing with \\b .

\\b is like \\< and \\> combined:

  • \\< match at beginning of word,
  • \\> match at end of word,
  • \\b match at the begining OR end of word,
  • \\B matches except at the beginning or end of a word.

Your source appears to be wrong, or at least incomplete. \\b matches any border, not just the front one. Quote 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

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

In case you are familiar with Perl regular expressions,

  • grep 's \\< is equivalent to Perl's (?<!\\w)(?=\\w)
  • grep 's \\> is equivalent to Perl's (?<=\\w)(?!\\w)
  • grep 's \\b is equivalent to Perl's \\b
  • grep 's \\b is equivalent to Perl's (?:(?<!\\w)(?=\\w)|(?<=\\w)(?!\\w))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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