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

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.

Note that the end anchor $ also behaves like \Z if the MULTILINE flag (will be discussed later) is not enabled in an input text with multiple lines. Thus, the preceding input string can also be matched using ^p+\n5{3}$.
主站蜘蛛池模板: 建水县| 驻马店市| 和硕县| 垣曲县| 阿拉尔市| 长垣县| 通道| 林甸县| 平罗县| 巴东县| 商都县| 邮箱| 将乐县| 潜江市| 商城县| 库尔勒市| 龙川县| 塔城市| 正安县| 句容市| 方城县| 全南县| 九江县| 灵台县| 开化县| 新晃| 石阡县| 麟游县| 进贤县| 如皋市| 伊川县| 汝南县| 建瓯市| 闻喜县| 馆陶县| 抚宁县| 神木县| 嘉峪关市| 定边县| 永城市| 西丰县|