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

Time:08-18

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

CodePudding user response:

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.

  • Related