官术网_书友最值得收藏!

Examples using boundary constructs

Which regex should be used to match "at" when the input is 'Hat at work"?

    \bat\b

The preceding regex should be used because \b (word boundary) stops the regex engine to match at in Hat, because \bat\b can match full words only.

What should be regex if we only want to match at in Hat but not the one that was matched in the preceding regex?

    \Bat\b

Now, this regex will match at that is a part of Hat because \B asserts a position that is between two word characters or a position between two non-word characters. Because of the presence of \B in the regex, it matches at only in Hat but not the word at.

If the input is suppress expression press depression, what will be the matches if the regex is \Bpress\B?

    suppress expression press depression

This is because \B matches the position between word characters, and the other instances, suppress and press, have non-word characters after press.

If the input is ppp\n555\n, then show the matched text using the following two regular expressions:

  • \Ap+\n5{3}\Z
  • \Ap+\n5{3}\z

Here are the matches:

A) ppp\n555
B) No match

The starting part, \Ap+\n5{3}, is common in both the regex patterns and matches ppp\n555 both the times. However, we are getting no match in the second case because of the subtle difference between the \Z and \z assertions. \Z asserts the position at the end or just before the last line terminator whereas \z always asserts the position at the very end. Due to the presence of \n at the end of the file, our second regex does not match. If we change the second regex to \Ap+\n5{3}\n\z, then it will match the entire input.

主站蜘蛛池模板: 宝丰县| 普陀区| 正蓝旗| 磴口县| 荆门市| 雷波县| 通江县| 都匀市| 宝兴县| 天台县| 许昌市| 岳阳市| 莱芜市| 闻喜县| 凉城县| 湘阴县| 安福县| 益阳市| 龙南县| 广平县| 盐源县| SHOW| 博罗县| 静宁县| 青浦区| 江永县| 志丹县| 宁海县| 攀枝花市| 荃湾区| 鄂伦春自治旗| 克拉玛依市| 宁南县| 东台市| 龙口市| 婺源县| 乐安县| 白山市| 郎溪县| 贵溪市| 界首市|