So, I am writing a short (and simple) regular expression, but I can think of two possible ways to do it. They both seem like good conventions, but I am not sure which one is better.
What I want to achieve with this pattern (ordered hierarchically):
- Speed
- Readability
- Sexyness
The regexp needs to match for one of two characters(let’s say they are # and ~) at the beginning of each line:
^[#~]^(#|~)Edit: since^#|~didn’t do what I wanted it, I corrected it.
I like both of them for different reasons (out of which most are aesthetic reasons); the bonus with the second one is that it’s a byte shorter.
Thanks!
^#|~is not the same as^[#~].^#|~will match a~in the middle of the string because|has a lower precedence than^. The correct way to express that is^(?:#|~)or^(#|~)if you don’t mind about the extra capture group.Comparing
^[#~]to the corrected regex^(?:#|~), I’d say the former totally wins out. (Normally a character class is more efficient than|because the latter is less specialized.)