Emmanuel N
Emmanuel N

Reputation: 7449

Regex to get words between two _

I would like a regex which will return words between _

   word1_word2_word3_word4_word5

I want one regex which will return word3 and another one which will return word4

I have this regex this _[^_]+_ start with but did not wok

Upvotes: 0

Views: 773

Answers (2)

Bohemian
Bohemian

Reputation: 425198

This regex will capture those words:

(?<(^|_))[^_]+(?=(_|$))

It's using a look behind for _ or start, and a look ahead for _ or end.
FYI, look behinds/aheads are non-consuming, ie "zero width".

Upvotes: 0

Flo
Flo

Reputation: 1965

Splitting the string by "_" and just accessing the chunks with index 2 and 3 may be easier and do just what you want.

Upvotes: 3

Related Questions