Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does 'Zero Or One Time'(?) quantifier return the correct result and an emty string

I have this pattern here:

string pattern = @".?";
string input = "o";

foreach (Match match in Regex.Matches(input, pattern))
  Console.WriteLine("<{0}> found at position {1}.", match.Value, match.Index);

For some reason it returns the following:

<o> found at position 0.
<> found at position 1.

Why does it return <> found at position 1. ?
I mean It already returned the <o> so it should not return anythiung else.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Well, pattern

.?

means

any symbol (.) zero or one times (?)

In your case – "o" – you have 2 matches:

o
^^
|| <- position 1: any symbol zero times (empty string)
| <-- position 0: any symbol one time ('o')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading