It seams that Both (a|e|i|o|u)? and [aeiou]? have the same effect, i wonder whether there is any significant difference on performance.
It seams that Both (a|e|i|o|u)? and [aeiou]? have the same effect, i wonder whether
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
in the example you give there is a significant benefit to using the class over the alternation.
For example:
appling either regex1 or regex2 to that string will fail however what goes on under the hood?
Regex1 takes the first character of string and see’s if it matches the regex so it checks ‘a’ against (a|e|i|o|u) which matches, however the regex engine also notes that there are 4 other alternations that could be tested if this one fails later on. It then takes the second character of string and matches it against the second atomic group of the regex ‘c’ This causes the regex to fail, however it still has 4 other ‘states’ it can use to try and make a match so the regex engine will go back a step and try to match the first character of the string against e i o u before deciding that the regex will fail completely and exit.
Regex 2 on the other hand decides that the first character of the string ‘a’ is one of the characters in the class [aeiou] no further states are created and therefore when the second character fails to match it exits with a failure, much quicker than regex1.
There is a lot more to how regex internals work as there are two types of engine (deterministic and non deterministic) but if you’re interested in reading more regular-expressions.info has a very good detailed description of what is going on.