谁还没个明天 2012-08-08 22:38 采纳率: 50%
浏览 129
已采纳

W 和 b 正则表达式元字符的区别

Can anyone explain the difference between \w and \b regular expression meta-characters?

It is my understanding that both these meta-characters are used for word boundaries. Apart from this, which meta character is efficient for multi lingual content?

转载于:https://stackoverflow.com/questions/11874234/difference-between-w-and-b-regular-expression-meta-characters

  • 写回答

5条回答 默认 最新

  • 三生石@ 2012-08-08 23:50
    关注

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

    In all flavors, the characters [a-zA-Z0-9_] are word characters. These are also matched by the short-hand character class \w. Flavors showing "ascii" for word boundaries in the flavor comparison recognize only these as word characters.

    \w stands for "word character", usually [A-Za-z0-9_]. Notice the inclusion of the underscore and digits.

    \B is the negated version of \b. \B matches at every position where \b does not. Effectively, \B matches at any position between two word characters as well as at any position between two non-word characters.

    \W is short for [^\w], the negated version of \w.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改