Matevz
Matevz

Reputation: 23

regex for pattern before a certain word

I am having problems finding a right RegEx for recognizing the word before a certain word (but without the latter word). Below is the pattern, where W1 is dynamically asigned word.

   Set regExp = New RegExp 
    With regExp
        .Pattern = "\S* " + W1
        .IgnoreCase = True
        .Global = True

This pattern works but i get the whole pattern. If I have such sentence :"Today is a sunny day" and W1 is set as "day", this pattern gives me "sunny day".

I would like to get just sunny, without the W1. Is this possible?

Thanks,m

Upvotes: 2

Views: 112

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174696

Put W1 inside a positive lookahead assertion.

.Pattern = "\S+(?= *" + W1 + ")"

Upvotes: 1

Related Questions