1

Wondering if someone can tell me why this regular expression doesn't work.

Expression -> ^[A-Za-z0-9$&!#-_?:;\"']+$

The problem is, it's matching against characters not in the set. For example, the word match properly matches and the word match~ does not, but match@ and match! incorrectly match.

I'm using java to match it, and the matching should be fairly straight forward with the code below:

        RE re = new RE(expression);
        return re.match(value);

I know it's probably something ridiculously simple that I'm missing, but if anyone has any thoughts on it, I'd greatly appreciate it!

2
  • According to your expression match! is an acceptable value. Commented Feb 24, 2012 at 16:37
  • This begs the question 'Why would match pass the test?' According to you m is not in the character class. You seem to understand the syntax of classes. You even seem to know about assertions, quantifiers and such.. Commented Feb 24, 2012 at 16:52

2 Answers 2

2

I suspect its #-_ that is acting up. Escape the - and see if that helps.

New expression would be ^[A-Za-z0-9$&!#\-_?:;\"']+$

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

Comments

1

The problem is the hyphen - in your character set. Your are accepting characters from # to _.

Change it to ^[-A-Za-z0-9$&!#_?:;\"']+$ or escape -.

1 Comment

Bruno, this was exactly the problem. Thank you very much.

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.