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 match 10 characters after second pattern

I would like to match 10 characters after the second pattern:

My String:

www.mysite.de/ep/3423141549/ep/B104RHWZZZ?something

What I want to be matched:

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

B104RHWZZZ

What the regex currently matches:

B104RHWZZZ?something

Currently, my Regex looks like this:

(?<=\/ep\/)(?:(?!\/ep\/).)*$.

Could someone help me to change the regex that it only matches 10 characters after the second "/ep/" ("B104RHWZZZ")?

>Solution :

It depends on which characters you allow to match. If you want to allow 10 non whitspace characters characters not being / or ? then you could use;

(?<=\/ep\/)[^\/?\s]{10}(?=[^\/\s]*$)

Explanation

  • (?<=\/ep\/) Assert /ep/ directly to the left
  • [^\/?\s]{10} Match 10 times any non whitespace character except for / and ?
  • (?=[^\/\s]*$) Assert no more occurrence of / to the right

Regex demo

Or matching 1+ chars other than / ? & instead of exactly 10:

(?<=\/ep\/)[^\/?&\s]+(?=[^\/\s]*$)

Regex demo

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