Comments
Usually regular expressions are very long and could be almost unreadable. Even if you wrote it by yourself sometime ago. In such cases comments are very useful. To use it you need to turn on free-spacing mode by adding x
modifier to your expression. Comment starts with a hashtag #
. Let’s parse date in this format: YYYY/MM/DD (year should be between 1000 and 2012 and assume all months have 30 days).
And the same regex with comments:
Lookahead assertions
Sometimes it’s needed to match something which is followed by something else. But not include this following something else in the match. For such cases lookahead assertion is what you need. There are two types of lookahead assertion:
- positive
(?=regex)
- matches only if it is followed by regex - negative
(?!regex)
- matches if it is followed NOT by regex
Let’s have a look on a simple example.
Let’s say you have a list of files:
and you want to find all names of xml files but do not include the .xml part. Of course you can use simple pattern ^[a-z]+\.xml$
but then the .xml part will be included in the match. To have only filename use this pattern: ^[a-z]+(?=.xml$)
.
Some explanation:
You can play with it here: https://regex101.com/r/tK4lU3/1
Result would be following:
Lookbehind assertions
Lookbehind assertion works the same way but looks before the current match. And there are two types of it as well: positive (?<=regex)
and negative (?<!regex)
.
Almost the same example, but now let’s find all the file extensions we have for filename model.
Pattern for it would be: (?<=^model\.)[a-z]+$
and the result:
If-then-else conditions
To check some conditions regular expressions provide this functionality. The syntax is (?(?=regex)then|else)
or if you don’t have else just (?(?=regex)then)
.