Jithu
Jithu

Reputation: 511

Regular Expression for a valid Text

I was trying to create a regular expression which validate !!

  1. If the text has only special character(s), or spaces , It should validate false ...

Ex : @#$##

  1. If it has some text along with special characters at the end, it should return true...

EX : Test!!

  1. If it is starting with a special character apart from space or . It should validate to false.

Ex : @#Test

  1. If the special character, except space or coma, comes in between. it should validate to false !!

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

  1. Testing with Fischermaen's Answer - ^(\s|\w)\w*\W*$

sdf SDF! : Validate to false

___ : validate to true (Under scores)

sadf,sdfsdf : validate to false

Empty Spaces : Validate to true

Upvotes: 2

Views: 4573

Answers (3)

FailedDev
FailedDev

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

piebie
piebie

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

Fischermaen
Fischermaen

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

Related Questions