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

Regex to fetch string after character and before word

I’m trying to code a regex to fetch a portion of a string that is between a character and a word.

E.g.

FIRST/SECOND/999999/MATCH EVERYTHING HERE BEFORE third

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

It should fetch the word between "999999/" (this section will always be numeric, 6 digit, and end with a forward slash) and "third" (this word does not change).
Expected result: MATCH EVERYTHING HERE BEFORE

I ended up with the following Regex, but it only fetches the word if there’s a whitespace before the word ‘MATCH’:

/(?<=/\s).*(?=\sthird)/

Can anyone help?

Thanks!

Edit: updated for clarification. Thanks @JvdV

>Solution :

It looks like the following should work:

(?:^|\/)\d{6}\/([^\/]+?)\s*\bthird

See an online demo


  • (?:^|\/) – Non-capture group for start-line anchor or forward slash;
  • \/ – A literal forward slash;
  • ([^\/]+?) – Capture group to catch 1+ (lazy) characters other than forward slash;
  • \s*\bthird – 0+ Whitespace characters, a word-boundary and literally the word third.
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