开发者

Excluding strings using regex

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


^(?:(?!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.


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.


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).*).*$
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜