- 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.
- JavaScript百煉成仙
- 云計算通俗講義(第3版)
- Hands-On Automation Testing with Java for Beginners
- 軟件測試技術指南
- Getting Started with Gulp
- INSTANT Yii 1.1 Application Development Starter
- Raspberry Pi Robotic Blueprints
- 平面設計經典案例教程:CorelDRAW X6
- Python Digital Forensics Cookbook
- 奔跑吧 Linux內核
- 超好玩的Scratch 3.5少兒編程
- Instant GLEW
- Drupal 8 Development Cookbook(Second Edition)
- Learning Cocos2d-JS Game Development
- C++17 By Example