0

I am looking for a Regular expression to match only numeric strings with special characters and of size 4+. I did a little review on the questions posted in here:

Testing website: http://regexlib.com/RETester.aspx


1- regular expression for numeric and special chacter check

Solution: ^((?!.*?\d)(?!.*?[^\w\s]).)+$

I tried this regular expression on:

  • 155-555: No match (wrong)
  • 155555: No match (wrong)

2- Regular Expression to match a string of numbers including special characters

Solution: ([-()_.+ ]*\d[-()_.+ ]*){4,}

  • 155-555: match (correct)
  • 155555: match (correct)
  • 155-555aaa: match (wrong)

This regular expressions matched the previous strings. But there is an error. The third string 155-555aaa contains alphabet letters and not special characters. We should have no match for 155-555aaa since a is not a special character. How can I modify this regular expression to only match numeric string with special characters. The numeric string can be of any form:

example: 24332-2432@2342-1234


Special Characters : [&~#"{'[(|-`_\ç^à@)]}=}^¨$$*%ù]

numeric = digits

3
  • 4
    What's a "special character"? Is ぷ special? Commented Jan 28, 2014 at 11:28
  • ok Special Characters: [&~#"{'[(|-`_\ç^à@)]}=}^¨$$*%ù] Commented Jan 28, 2014 at 11:29
  • How about How about: [^a-zA-Z]{4,}? Commented Jan 28, 2014 at 11:29

2 Answers 2

5

Just match anything that isn't [a-zA-Z] (with a length of 4 or more):

^[^a-z\s]{4,}$

Here is a test on RegexPal.

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

4 Comments

Yes, hence [^a-z] as opposed to [a-z]
He negates all letters, but also matches the beginning (^) and the end ($) of the string, which is how he prevents "155-555aaa" from being a match.
Note that spaces will also match this negation, so (3 4 5 6 7 8) is a match, as are (1 2) and even just a bunch of spaces.
@Dane thanks for the heads up. I've edited my answer so the regex now rejects whitespace
1

With the update special characters=[&~#"{'[(|-``_\ç^à@)]}=}^¨$*%ù] the class for "special or digit" becomes just [0-9&~#"{'[(|-``_\ç^à@)]}=}^¨$*%ù] and the full regex becomes ^[0-9&~#"{'[(|-``_\ç^à@)]}=}^¨$*%ù]{4,}$.

But it's rather suspicious that ù is special and ú isn't.

Comments

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.