The "Single character alternation" warning shows up in JetBrains' IDEs. The warning reports that the single character alternation (such as a|b|c
) in a regular expression would be better written as a character class instead (i.e. [abc]
). This might even be slightly more performant when it's matching.
For example, consider the following regular expression that allows one of the symbols from; =
, :
, -
or _
:
=|:|-|_
This could be re-written as:
[=|:|\-|_]
Please keep in mind that in character classes, the characters ^-]\
need to be escaped.
This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.