Matt Elhotiby
Matt Elhotiby

Reputation: 44086

How do I find all the words until a certain word with regex?

I have this body content below

[url=undefined][i][/i] John Doe wrote:[/url] Some other random wrote
[url=undefined][i][/i] John Doe Junior wrote:[/url] random stuff again
[url=undefined][i][/i] John Doe Junior III wrote:[/url] random stuff wrote
[url=undefined][i][/i] John wrote:[/url] random stuff wrote

I am trying to pull out the names John Doe, John Doe Junior, John Doe Junior III and John using regex. I have it almost working but I can't seem to get over the hump

My Regex fails when there is a single name

/undefined\]\[i\].\[\/i\].(\w+.+?)?.wrote/

Any ideas how I can get the matching groups, of any name before the wrote word and not greedy

My test URL is here https://regex101.com/r/pJPbvt/2

Upvotes: 0

Views: 102

Answers (1)

user557597
user557597

Reputation:

Something along these lines ..

\[/i\]\s*(\S+(?:\s+\S+)*?)\s*wrote:

https://regex101.com/r/dGMd0b/1

 \[/i\]  
 \s* 
 (                             # (1 start)
      \S+ 
      (?: \s+ \S+ )*?
 )                             # (1 end)
 \s* wrote:

Upvotes: 1

Related Questions