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

Is it possible to apply REGEX rules on already matched cases?

Is it possible to create some rules in REGEX that applies over the already matched cases?

For example:
I have this string: Mark Marcus Miami 36yo
I need to match only: Mark Marcus Miami
I used (.*) and it matches: Mark Marcus Miami

I’m wondering if I can somehow apply another REGEX rule ON THE MATCHED CASE to eliminate the space at the end of my initial match. Someway to say: remove last character of all matches you found so far

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 :

Assuming that the full name could possibly end with a 36yo or similar age term, and you don’t want that term if it does appear, you could stop matching using a positive lookahead:

^(\w+(?: \w+)*)(?=\s+\w+$)

This pattern matches a series of names words, except for the last word term in the input. Here is a working 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