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

How to match part of the string contains certain word

I want to be able to match the following texts

top of radio 54 / bottom 27
radio top 54 / bottom 27

The word top can be before or after radio only for the first half of the text (before /). top that appears after / should not be matched.

I tried to use the following pattern that encloses the lookahead for the first half with parentheses.

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

((?=top).*radio\s\d{2}\s)\/\D*bottom\s\d{2}

But it doesn’t work like I expected. Only matches first string, but not second.

>Solution :

You may use this regex with a lookahead:

^(?=[^/]*\btop\s)[^/]*radio[^/]+\d{2}\s+/\D*bottom\s+\d{2}$

RegEx Demo

RegEx Breakup:

  • ^: Start
  • (?=[^/]*\btop\s): Lookahead to assert presence of word top being present before matching a /. This is to ensure we match word top before / only
  • [^/]*: Match 0 more of any char that is not a /
  • radio: Match radio
  • [^/]+: Match 1+ of any char that is not a /
  • \d{2}: Match 2 digits
  • \s+: Match 1+ whitespaces
  • /: Match a /
  • \D*: Match 0 or more non-digits
  • bottom: Match bottom
  • \s+: Match 1+ whitespace
  • \d{2}: Match 2 digits
  • $: End
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