1

Yep, another regex (javascript implementation) question...

I can't figure out how to find the string 'cat' as long as it occurs anywhere ahead of 'dog'.

So for the following sentence...

cat categorically hates the dog, im going to mention cat again. 

The first and second occurrences will be found, not the last one.

5
  • 1
    Judging from your example (and the accepted answer) you probably want "substrings", not "words". Commented Oct 4, 2013 at 0:25
  • The accepted answer is incorrect as it does not find the instances of cat which are ahead of dog. My answer below finds the instances of cat which are ahead of dog. Commented Oct 4, 2013 at 0:31
  • @Fresh - as I mentioned below, you need to explain this. "Ahead of" and "before" mean the same thing, unless there's a usage I haven't heard of. Commented Oct 4, 2013 at 0:34
  • @jwismar A synonym of "ahead" is "in front" (google for "define ahead"), so this could be re-worded "'cat' as long as it occurs anywhere in front of 'dog'". Hence the reason why I expected to see a regex which finds the last instance of cat i.e. the one in front of dog. Commented Oct 4, 2013 at 0:40
  • "In front," "before," or "ahead of," in this context all mean "toward the beginning." Commented Oct 4, 2013 at 0:41

2 Answers 2

3

You need to use a lookahead:

/cat(?=.*dog)/

matches any "cat" that is followed anywhere by a "dog", taking into account that there may be other characters in between.

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

3 Comments

@Z-Mehn This is incorrect as your question asked for instances of cat which occur ahead of dog. This regex finds instances of cat which occur before dog. Also the second instance of car found by this in not a word, its a substring of "categorically".
@Fresh - you need to explain why "ahead of" is different from "before". You're using the words in a way that I don't understand.
See my comment on the question.
0

Your questions states:

I can't figure out how to find the word 'cat' as long as it occurs anywhere ahead of 'dog'.

The regular expression for this is:

cat(?!.*dog)

This uses negative lookup to find cat which is not followed by dog.

6 Comments

@thg435 because this regex finds instances of cat which are not followed by dog. Hence when applied to the string in the question it will find the last instance of cat (i.e. the instance of cat ahead of dog)
He asked for instances of "cat" that appear ahead of dog, which means before dog. Your regex does the opposite.
Yes, I think we experince a kind of cognitive dissonance here. We use a look_ahead_ to find out what's behind the string. +1 for making me think of it. ;)
@jwismar See my comment on the question
@thg435 Ah ha, I understand Fresh's confusion now. Now that you mention it, "lookahead" in the regex sense is a bit confusing, isn't it? Fortunately, Z-Mehn described what he was looking for right there in his question, ("The first and second occurrences will be found, not the last one.") which is the everyday use of "ahead of", not the regex lookahead sense.
|

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.