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

Wildcard around string

I am trying to match regex
.*(bob|alice).*
against bob and alice like to text each other

I expected 2 matches, but only "alice" is matched.

enter image description here

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

When removing the wildcards, then bob and alice are matched.
(bob|alice).

enter image description here

Question: why adding .* changes the behaviour? I thought it means zero or any characters?

>Solution :

The behavior you are seeing is expected, given that the leading .* in your pattern is greedy, and therefore will match all content up to the last bob or alice. Most likely, you can get away with:

\b(?:bob|alice)\b
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