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....


hamida khanam91 reputationPosted Mar 11, 2011, 1:00 AM EST
this is verbose strings feature of c# and here @ you dont need escape sequence to for escaping special characters.
Smart LuckyRank #2,625 · 531 reputationPosted Mar 11, 2011, 1:29 AM EST
VulpesRank #55 · 29.2K reputationPosted Mar 11, 2011, 1:43 AM EST
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.
Raja KrishnamurthyRank #3,615 · 403 reputationPosted Mar 11, 2011, 2:31 AM EST
VulpesRank #55 · 29.2K reputationPosted Mar 11, 2011, 4:01 AM EST
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 KrishnamurthyRank #3,615 · 403 reputationPosted Mar 11, 2011, 10:13 AM EST
Suthish NairRank #92 · 22.8K reputationPosted Mar 12, 2011, 8:46 AM EST