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

Matching an overlapping pattern in regex

In the following string:

7thousand3hundred54

I want to match:

7thousand3
3hundred54

I’m currently using this pattern:

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

(\d?[(1-9)]|[1-9]0)(hundred|thousand)(\d?[(1-9)]|[1-9]0)

Which matches just:

7thousand3

Which makes sense, but how can I modify the pattern such that the 3 in the middle overlaps in the two matches that I’m looking for in 7thousand3hundred54?

>Solution :

You’ve to use positive lookahead assertion, to get it to work. The simplest solution is:

(?=(\d+(hundred|thousand)\d+))

If you want the extra digit checks you had, you can use:

(?=((\d?[(1-9)]|[1-9]0)(hundred|thousand)(\d?[(1-9)]|[1-9]0)))

Regex101

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