- Java 9 Regular Expressions
- Anubhava Srivastava
- 326字
- 2021-07-02 18:58:36
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.
- VMware View Security Essentials
- CockroachDB權(quán)威指南
- C#完全自學教程
- Mastering Entity Framework
- FLL+WRO樂高機器人競賽教程:機械、巡線與PID
- TradeStation交易應(yīng)用實踐:量化方法構(gòu)建贏家策略(原書第2版)
- Mastering ROS for Robotics Programming
- Kotlin極簡教程
- Java 9 with JShell
- Python面試通關(guān)寶典
- 游戲設(shè)計的底層邏輯
- 創(chuàng)新工場講AI課:從知識到實踐
- 菜鳥成長之路
- Django 3 Web Development Cookbook
- OpenStack Sahara Essentials