if (Regex.IsMatch("testtest", @"^\w+$")) { // Do something here }
Why @ sign is used here....?
string pattern = "^a+$";
if (Regex.IsMatch(content, pattern))
Console.WriteLine("Pattern Found");
else
Console.WriteLine("Pattern Not Found");
and in this @ sign is not used why......?
if(Regex.IsMatch(inputString, @"(?
{
in above expression ?
Any one who can help me....

Suthish NairPosted Mar 12, 2011, 1:46 PM
Raja KrishnamurthyPosted Mar 11, 2011, 3:13 PM
VulpesPosted Mar 11, 2011, 9:01 AM
I've seen this question come up a few times on C# forums over the years so, hopefully, my explanation is improving :)
Incidentally, we don't seem to have answered the second part of the OP's question.
When you have something like this: (?
it means that everything else within the enclosing brackets (...) - except ?
string inputString = "239.240.241.242";
Match m = Regex.Match(inputString, @"(?
if (m.Success)
{
MessageBox.Show(m.Groups["First"].Value); // 239
}
Raja KrishnamurthyPosted Mar 11, 2011, 7:31 AM
VulpesPosted Mar 11, 2011, 6:43 AM
Now, if you used this in an ordinary C# string, then the compiler would think it was an escape character (\n, \t, \r etc) and give an error because there is no such escape character as \w.
To avoid this, you need to prefix the string with @ which tells the compiler to treat everything in it literally i.e. read \w as being a backslash followed by a 'w'. Such strings are technically called 'verbatim string literals'.
To match numbers, replace \w in your regular expression with \d which repesents any digit 0-9.
Smart LuckyPosted Mar 11, 2011, 6:29 AM
hamida khanamPosted Mar 11, 2011, 6:00 AM
this is verbose strings feature of c# and here @ you dont need escape sequence to for escaping special characters.