I'm trying to code a regex to fetch a portion of a string that is between a character and a word.
E.g.
FIRST/SECOND/999999/MATCH EVERYTHING HERE BEFORE third
It should fetch the word between "999999/" (this section will always be numeric, 6 digit, and end with a forward slash) and "third" (this word does not change). Expected result: MATCH EVERYTHING HERE BEFORE
I ended up with the following Regex, but it only fetches the word if there's a whitespace before the word 'MATCH':
/(?<=/\s).*(?=\sthird)/
Can anyone help?
Thanks!
Edit: updated for clarification. Thanks @JvdV
CodePudding user response:
It looks like the following should work:
(?:^|\/)\d{6}\/([^\/] ?)\s*\bthird
See an online demo
(?:^|\/)- Non-capture group for start-line anchor or forward slash;\/- A literal forward slash;([^\/] ?)- Capture group to catch 1 (lazy) characters other than forward slash;\s*\bthird- 0 Whitespace characters, a word-boundary and literally the word third.
