在Java中使用\b边界匹配器 [英] Use of \b Boundary Matcher In Java

查看:260
本文介绍了在Java中使用\b边界匹配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Oracle文档中的边界匹配器。我理解大部分内容,但我无法掌握 \b 边界匹配器。以下是文档中的示例。

I am reading Boundary Matcher from Oracle Documentation. I understand most of the part, but i am not able to grasp the \b Boundary Matcher. Here is the example from the documentation.


检查模式是否在单词边界开始和结束(而不是
到一个较长字符串中的子字符串),只需在两边使用\ b;例如
,\ bdog \ b

To check if a pattern begins and ends on a word boundary (as opposed to a substring within a longer string), just use \b on either side; for example, \bdog\b

输入你的正则表达式:\ bdog \b输入要搜索的输入字符串:狗玩
在院子里。我发现文本dog从索引4开始,到
索引7结束。

Enter your regex: \bdog\b Enter input string to search: The dog plays in the yard. I found the text "dog" starting at index 4 and ending at index 7.

输入你的正则表达式:\ bdog \b输入输入字符串到搜索:小狗
在院子里玩。找不到匹配项。要匹配
非字边界上的表达式,请改为使用\B:

Enter your regex: \bdog\b Enter input string to search: The doggie plays in the yard. No match found. To match the expression on a non-word boundary, use \B instead:

输入正则表达式:\ bdog \B输入输入字符串到搜索:狗
在院子里玩。找不到匹配。

Enter your regex: \bdog\B Enter input string to search: The dog plays in the yard. No match found.

输入您的正则表达式:\ bdog \B输入要搜索的输入字符串:小狗
在院子里播放。我发现文本dog从索引4开始,
从索引7开始。

Enter your regex: \bdog\B Enter input string to search: The doggie plays in the yard. I found the text "dog" starting at index 4 and ending at index 7.

简而言之,我不是能够理解\ b的工作。有人可以帮我描述它的用法,并帮助我理解这个例子。

In short, i am not able to understand the working of \b. Can someone help me describing its usage and help me understand this example.

谢谢

推荐答案

\b 是你可以称之为锚点的东西:它将匹配输入文本中的位置。

\b is what you can call an "anchor": it will match a position in the input text.

更具体地说, \b 将匹配输入文本中的每个位置:

More specifically, \b will match every position in the input text where:


  • 没有前面的字符,后面的字符是单词字符(任何字母或数字,或下划线);

  • 没有后面的字符和前面的字符字符是单词字符;

  • 前面的字符是单词字符,后面的字符不是;或

  • 以下字符是单词字符,前面的字符不是。

  • there is no preceding character and the following character is a word character (any letter or digit, or an underscore);
  • there is no following character and the preceding character is a word character;
  • the preceding character is a word character and the following character is not; or
  • the following character is a word character and the preceding character is not.

For例如,文本我的狗吃中的正则表达式 dog \b 将匹配 g dog (这是一个单词字符),在下面的空格之前(不是)。

For instance, the regex dog\b in the text "my dog eats" will match the position immediately after the g of dog (which is a word character) and before the following space (which is not).

请注意,与所有锚点一样,它与位置匹配的事实意味着它使用任何输入文本。

Note that like all anchors, the fact that it matches a position means that it does not consume any input text.

其他锚点是 ^ $ ,外观。

这篇关于在Java中使用\b边界匹配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆