NewBie
NewBie

Reputation: 1844

Regular Expression to check a string with special characters

I need to check whether my string got only

alphabets of the form

or of the form - 'Text'

or of the form - "Text"

How to frame a regex for that? Currently what im using is provided below and that seems not working. Please help me modify this. Thanks in advance.

      Regex isString = new Regex("[^a-zA-Z]|[^']|[^\"]");

Upvotes: 0

Views: 2855

Answers (2)

Jerad Rose
Jerad Rose

Reputation: 15503

Regex isString = new Regex("^['][a-zA-Z]*[']|[\"][a-zA-Z]*[\"]|[a-zA-Z]*$");

Upvotes: 1

Jayantha Lal Sirisena
Jayantha Lal Sirisena

Reputation: 21366

Regex isString = new Regex("(['\"]?)[a-zA-Z]+\\1$");

this will match "text", 'text' and not with 'text"

Upvotes: 2

Related Questions