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 a specific string between foward slash or # using regex

I’m trying to make this regex:

(?<=section)(.*?)(?=\#)

For these examples:

https://www.test.com/en/section/string-to-get#id=4949
https://www.test.com/en/section/string-to-get/page&2#id=4949

current regex

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 :

You need to use

(?<=section\/)([^\/#]*)

Or, just

section\/([^\/#]*)

and grab Group 1 value.

Here,

  • (?<=section\/) – a positive lookbehind that matches a location immediately preceded with section/ substring
  • ([^\/#]*) – Capturing group 1: zero or more chars other than / and #.

See the regex demo #1 and regex demo #2.

Depending on whether or not regex delimiters are required and if they are not /s you may use an unescaped /, (?<=section/)([^/#]*) and section/([^/#]*).

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