1

I am trying to match 3 words that can appear anywhere in the string:

Win
Enter
Now

All 3 words must exist in the string for it return as a match. But I am having issues for getting a match when all 3 words do exist.

Below is the regex I am using: http://regexr.com/39b83

^(?=.*?win)(?=.*?(enter))(?=.*?(now)).*

Regex is working when all three words are within the same line... when its spread out across the entire string on different lines, it is failing to match.

Any direction or help is appreciated.

11
  • And what issues are you having? (Are you trying to match any of those words, rather than all of them?) Commented Aug 18, 2014 at 3:38
  • It is returning no match even when the string includes all three words. Commented Aug 18, 2014 at 3:39
  • Can you give an example of a string that you tried, please? It works fine for me – even on RegExr. Commented Aug 18, 2014 at 3:40
  • If this is something quick and dirty, I'd just run several greps. grep win filename.txt | grep enter | grep now Commented Aug 18, 2014 at 3:47
  • @minitech, try this regexr.com/39b83, I am seeing no match in the example... Commented Aug 18, 2014 at 3:49

5 Answers 5

2

Since you don't want to match words like center (with the word "enter"), I would use:

/(\benter\b)|(\bwin\b)|(\bnow\b)/

Link to Fiddler

Sign up to request clarification or add additional context in comments.

5 Comments

My apologies, I left out the requirements that all 3 works MUST exist.
@Rick in which order ? if you don't know the order it'd be easier to run each word separately
in no particular order. the issue I am having is primarily when the three words are not in the same line it does not find a match...
@Rick may i know the expected output for the above input string?
@Rick if you don't know the order and you want to make sure all the three words are present, there are 3! options that you have to check, that's not a very complicated regex - just a very long one. It'll be easier to read and maintain if you'll check each word separately like I suggested on the comment above.
1

I think C# would support (?s) DOTALL modifier. If yes then you could try the below regex,

(?i)(?s)win.*?enter.*?now

6 Comments

My apologies, I left out the requirements that all 3 works MUST exist.
@AvinashRaj it won't work if the order of the words is different
correct, as the order may be different, was wondering if this was possible through regex before iterating programatically with a single pattern.
@AvinashRaj: =) It works. Taking your comment above into consideration if the order is unknown and may change is it better to programmatically iterate?
if the order is unknown, you may use (?i)(?s)(?:win|enter|now).*?(?:win|enter|now).*?(?:win|enter|now) but it also matches win foo win foo bar win
|
1

How about...

/(win|enter|now)/gi

regex diagram

1 Comment

My apologies, I left out the requirements that all 3 works MUST exist.
0

It sounds like you want to match the lines on which these words appear, across up to three lines. That’s not really easy, but:

/^.*win.*(?:\s+.*)?enter.*(?:\s+.*)?now.*|^.*win.*(?:\s+.*)?now.*(?:\s+.*)?enter.*|^.*enter.*(?:\s+.*)?win.*(?:\s+.*)?now.*|^.*enter.*(?:\s+.*)?now.*(?:\s+.*)?win.*|^.*now.*(?:\s+.*)?win.*(?:\s+.*)?enter.*|^.*now.*(?:\s+.*)?enter.*(?:\s+.*)?win.*/igm

should do it.

Comments

0

It 's because the dot doesn't match the newline character. To change this, you have to ways. The first, use the s modifier (that allows the dot to match newlines):

(?s)^(?=.*\bwin\b)(?=.*\benter\b)(?=.*\bnow\b).*

But this feature isn't always available (for example in Javascript). The second way consists to replace the dot with [\s\S] (a character class that matches all the characters):

^(?=[\s\S]*\bwin\b)(?=[\s\S]*\benter\b)(?=[\s\S]*\bnow\b)[\s\S]+

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.