views:

64

answers:

4

I am completely new to writing regular expressions. I am trying to write a Regex that will not allow the following terms in a text box.

the
The
T h e
+1  A: 

/t ?h ?e/i will match any of the variations above (and others such as T hE).

How you reject input that matches this pattern depends on the language and libraries you are using.

Sinan Ünür
A: 

It depends what language you are using, but you can specify case-insensitivity.

if you want a regex that checks to see if it exists try:

(the|t\sh\se)

Roo
That character class is not doing what you think it is doing.
Sinan Ünür
Your second expression won’t work. `[^(the|t\sh\se)]` means *one characters except `(`, `t`, `h`, `e`, `|`, `\s`, `)`*.
Gumbo
good catch! thanks!
Roo
A: 

I recommend using http://www.regular-expressions.info they have a very good quickstart guide and a very through tutorial. It helped me a lot when I first was learning Regular expressions.

Scott Chamberlain
+3  A: 

If you want to match on things like 'WHAT H ELL' then simply rip all the whitespace from the string so you get 'WHATHELL' and then look for 'THE'

If you don't include those situations, then use this regex with case sensitivity turned off

\bt\s*h\s*e\b

result

WHAT H E - fail
The - success
T he - success
th E - success
t h e - success
the - success
them - fail
hasthem - fail
has them - fail
Chad
Chad your solution works good however i think its the editor problem, i think you actually said "t\s*h\s*e" and i think the /b and \b were appended because of the editor. So "t\s*h\s*e" is the answer...
No, I added the \b\b as they are word boundaries. To differentiate "This is the sentence." having the word 'the' from, the sentence "I'll go with them too." with the word 'them' Without the \b around the phrase, it will match on the word 'them'.This is why you should download and use expresso to write and test regexes.
Chad
Hey thanks for responding back, BTW how can i turn off Case sensitivity?
new Regex("\\bt\\s*h\\s*e\\b", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
Chad