Regular Expressions
Regular expressions are a very powerful tool. However, they have their downsides, mainly in maintainability. Regexes are often really hard to read or modify, as they are often just a long string of characters with special meaning.
For that reason, we should use them with care in our applications. But that is not the only place where they have their use. Modern editors come with support for searching and replacing using regexes. This is a perfect use case, because, as a one-time action we need not maintain them. We can also see the exact context that it will be applied to, so there are no bug issues.
The Challenge
Keeping our regex toolbox sharp is important, and I recently got a fun regex challenge to practice. In Visual Studio Code paste the following and create a regular expression that matches each of the good lines (it should show 6 matches), and none of the bad lines:
GOOD: "xxx";yyyy;"zzz" "xxx;xxx";yyyy xxxx;yyyy;zzzz "xxx";"yyyy";"zzzz" xxxx;;yyyy;zzz xxx;"yy""yy";"zzzzz" BAD: xxx";"yyyy xx"xx";yyyy xxxx;"yy"; xx";yyyyy";zzzz" xxxxx;";yyyyy "xxxx";";"yyyy"
Cheat Sheet
To save you some time here is a cheat sheet of VSCode regex:
Regex | Meaning |
---|---|
. |
Any character |
[a-zA-Z_] |
A character in the range a-z or A-Z or _ |
[^a-zA-Z_] |
A character _not_ in the range a-z nor A-Z nor _ |
X* |
Zero or more X (longest match) |
X*? |
Zero or more X (shortest match) |
X+ |
One or more X (longest match) |
X+? |
One or more X (shortest match) |
X|Y |
Either X or Y |
^ |
Start of line |
\r?$ |
End of line |
No comments:
Post a Comment