Question Marks in Regular Expressions
I'm Reading the Regular Expressions Reference and I'm Thinking About? and? ? Characters. Could You Explain Me with Some Examples Their Usefulness? I Don't...
I'm reading the regular expressions reference and I'm thinking about ? and ?? characters. Could you explain me with some examples their usefulness? I don't understand them enough.
thank you
5 Answers
This is an excellent question, and it took me a while to see the point of the lazy ?? quantifier myself.
? - Optional (greedy) quantifier
The usefulness of ? is easy enough to understand. If you wanted to find both http and https, you could use a pattern like this:
https?
This pattern will match both inputs, because it makes the s optional.