1

I was working on creating a regex which will include all patterns except 'time', 'hour', 'minute' so, worked this out:

^(?:(?!time).)*$

how do i add for the other 2 words also? I tried as follows but it is incorrect.

^(?:(?![time][hour][minute]).)*$

Is there a better way to do this?

I am not adding the values which can be accepted as it ranges from numbers to alphabets to symbols etc.

Please help.

Thank you

2
  • 2
    Please clarify your goal. If you're just trying to strip these words from the input, regex is not the best tool. If you're trying to select lines that don't contain these words, it can be done reasonably with a lookahead -- but it's not clear what you want. Commented Sep 25, 2010 at 3:34
  • possible duplicate of A regular expression to exclude a word/string Commented Aug 23, 2013 at 2:34

3 Answers 3

1
^(?:(?!time|hour|minute).)*$

| is alternation. This means that at all points in the string, we are not looking at any of those expressions (time, hour, or minute). [] is wrong because that creates a character class.

So it means, not looking at (a t, i, m, or e), then (a h, o, u, or r), etc.

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

Comments

0

I'm going to take a different approach here: what's wrong with !/time|hour|minute/? excessive use of regex is usually a bad idea.

Comments

0

From reading your question, I am guessing you only want "lines" that do not contain the words "time", "hour", or "minute". Try something like...

^(?!.*(time|hour|minute).*).*$

2 Comments

No, the OP's regex is correct (the first one, that is). The overall match is anchored, but the lookahead is free-floating; it gets applied at each position before the next character is consumed. It's a perfectly valid alternative to your approach, which is also correct (but I would get rid of that second .* inside the lookahead).
@Alan Moore - you are correct, my first statement was wrong (I've removed it), I think I was up to late and ?: seems to throw me.

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.