1

I have a regular expression as below. It should allow alphabets, digits, round brackets, square brackets, backslash and following punctuation marks: period, comma, semi-colon, full colon, exclamation, percentage and dash.

^[(a-z)(A-Z) .,;:!'%\-(0-9)(\\)\(\)[\]\s]+$

Question : I have tried this regular expression with some text at this online tester: https://regex101.com/r/kO5tW2/2, but it always comes up with no matches. What is causing the expression to fail in above case? To me, the string being tested should come back as valid, but it's not.

3
  • What's our expected output? Commented Aug 21, 2015 at 1:17
  • Your expression did not work. Just click on the online test url and verify. It says 'no matches'. Commented Aug 21, 2015 at 1:20
  • I tried escaping [, but still it says not valid. Commented Aug 21, 2015 at 1:28

2 Answers 2

1

Your spec does not mention a question mark. However, the test text you give does include a question mark. You could have tested this easily enough by removing one character at a time from the test text until you got a match, which would have happened when you removed the question mark.

Either add the question mark to the regexp, or remove it from your test test.

Also, you do not need to (and should not) enclose ranges in parentheses.

In the below, I've also removed escaping for characters which do not need to be escaped:

^[a-zA-Z .,;:!'%\-0-9\\()[\]\s?]+$
                              ^

https://regex101.com/r/kO5tW2/4

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

6 Comments

Great. Thanks so much. I am just verifying it in my aspx page.
Thanks. The expression you gave works perfectly. Would \" be for allowing double quotes as well?
When I use this expression in an aspx page I need to put it as Expression="^[a-zA-Z .,;:!'%\-0-9\()[]\s?]+$". So just putting a double quote in above results in parsing error at run time.
If I use " in place of " then the expression works to allow double quotes. Expression="^[a-zA-Z .,;:!'%\-0-9()[]\s?;quot;]+$"
In ASP.Net aspx page, we need to use " but in JavaScript we could just use double quotes as you suggested.
|
1

Try adding m (multiline) modifier to regex

If you have a string consisting of multiple lines, like first line\nsecond line (where \n indicates a line break), it is often desirable to work with lines, rather than the entire string. Therefore, all the regex engines discussed in this tutorial have the option to expand the meaning of both anchors. ^ can then match at the start of the string (before the f in the above string), as well as after each line break (between \n and s). Likewise, $ still matches at the end of the string (after the last e), and also before every line break (between e and \n). Source

enter image description here

5 Comments

Should not \s match all white space character like \n, so why is the multi-line modifier needed?
By default regex will match $ with end of line, not end of string
So will the multi-line expression be: ^[(a-z)(A-Z) .,;:!'%\-(0-9)(\)()[]\s]+$/gm
Well, this will not work. It will match lines within the input text, but not match the entire input text, which is presumably what the OP wants.
@Sunil: You have multiple redundant ( and ) in your regex.

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.