Reputation: 511
I was trying to create a regular expression which validate !!
Ex : @#$##
EX : Test!!
Ex : @#Test
Ex : te#$ sT
I apologize that, for this complex regular expression, my knowledge do not allow me to put any code which I tried. Because I got nothing apart from [a-b A-B !@#$%^&*()]
UPDATE
sdf SDF! : Validate to false
___ : validate to true (Under scores)
sadf,sdfsdf : validate to false
Empty Spaces : Validate to true
Upvotes: 2
Views: 4573
Reputation: 26930
Try this:
foundMatch = Regex.IsMatch(subjectString, @"^(?!\W*$)(?=[ .\w]\w*\W*$).*$");
I got a headache trying to figure out what you meant. You must learn to express yourself better.
Explanation:
"
^ # Assert position at the beginning of the string
(?! # Assert that it is impossible to match the regex below starting at this position (negative lookahead)
\W # Match a single character that is a “non-word character”
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
$ # Assert position at the end of the string (or before the line break at the end of the string, if any)
)
(?= # Assert that the regex below can be matched, starting at this position (positive lookahead)
[ .\w] # Match a single character present in the list below
# One of the characters “ .”
# A word character (letters, digits, etc.)
\w # Match a single character that is a “word character” (letters, digits, etc.)
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\W # Match a single character that is a “non-word character”
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
$ # Assert position at the end of the string (or before the line break at the end of the string, if any)
)
. # Match any single character that is not a line break character
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
$ # Assert position at the end of the string (or before the line break at the end of the string, if any)
"
Upvotes: 3
Reputation: 2672
As I understand it, you only want to accept strings that have some letters and then some special characters. So something like this:
[(a-zA-Z)+(\!\@\#\$\%\^\&\*\(\))+]
Upvotes: 1
Reputation: 12458
Here is my suggestion for you:
^(\s|\w)\w*\W*$
Explanation:
^(\s|\w) the first character has to be a whitespace or a alphanumeric (including underscore)
\w* followed by any number of alphanumeric (including underscore)
\W*$ at the end of the string any number of any *except* alphanumeric characters are allowed.
Upvotes: 1