regexp @ operator

  • Thread starter Thread starter puzzlecracker
  • Start date Start date
P

puzzlecracker

I understand that the following would split the line in words space
separated: string[] words = Regex.Split(text, @"\W+"); Why do we need
@ before regular expression?


Thanks
 
In C# the backslash (\) serves for escaping characters, and when you want to
include the literal backslash you have to escape it in one of two ways:

1) using the @ prefix which escapes all backslashes, or
2) by escaping the backslash using another one, e.g. "\\W+"

I personally prefer and recommend the @ prefix because I don't have to do it
for further slashes.
 
In C# the backslash (\) serves for escaping characters, and when you wantto
include the literal backslash you have to escape it in one of two ways:

1) using the @ prefix which escapes all backslashes, or
2) by escaping the backslash using another one, e.g. "\\W+"

I personally prefer and recommend the @ prefix because I don't have to doit
for further slashes.
--
Stanimir Stoyanovhttp://stoyanoff.info


I understand that the following would split the line in words space
separated: string[] words = Regex.Split(text, @"\W+"); Why do we need
@ before regular expression?

I see, I use @ to escape line-breaks, that seems another useful
application.

Thanks
 
One thing to note though, if you use the @ prefix line breaks will not work.
This is, essentially, because the line break is escaped and the string is
interpreted as 'slash, r, slash, n' and not 'carriage return, line break'.
--
Stanimir Stoyanov
http://stoyanoff.info

In C# the backslash (\) serves for escaping characters, and when you want
to
include the literal backslash you have to escape it in one of two ways:

1) using the @ prefix which escapes all backslashes, or
2) by escaping the backslash using another one, e.g. "\\W+"

I personally prefer and recommend the @ prefix because I don't have to do
it
for further slashes.
--
Stanimir Stoyanovhttp://stoyanoff.info


I understand that the following would split the line in words space
separated: string[] words = Regex.Split(text, @"\W+"); Why do we need
@ before regular expression?

I see, I use @ to escape line-breaks, that seems another useful
application.

Thanks
 
Stanimir said:
One thing to note though, if you use the @ prefix line breaks will
not work. This is, essentially, because the line break is escaped and
the string is interpreted as 'slash, r, slash, n' and not 'carriage
return, line break'.

Line breaks work fine.

But they look like this:

string s = @"
";

not this:

string s = @"\r\n";
In C# the backslash (\) serves for escaping characters, and when you
want to
include the literal backslash you have to escape it in one of two
ways: 1) using the @ prefix which escapes all backslashes, or
2) by escaping the backslash using another one, e.g. "\\W+"

I personally prefer and recommend the @ prefix because I don't have
to do it
for further slashes.
--
Stanimir Stoyanovhttp://stoyanoff.info


I understand that the following would split the line in words space
separated: string[] words = Regex.Split(text, @"\W+"); Why do we
need @ before regular expression?

I see, I use @ to escape line-breaks, that seems another useful
application.

Thanks
 
Thanks for pointing this out, Ben.

Ben Voigt said:
Stanimir said:
One thing to note though, if you use the @ prefix line breaks will
not work. This is, essentially, because the line break is escaped and
the string is interpreted as 'slash, r, slash, n' and not 'carriage
return, line break'.

Line breaks work fine.

But they look like this:

string s = @"
";

not this:

string s = @"\r\n";
In C# the backslash (\) serves for escaping characters, and when you
want to
include the literal backslash you have to escape it in one of two
ways: 1) using the @ prefix which escapes all backslashes, or
2) by escaping the backslash using another one, e.g. "\\W+"

I personally prefer and recommend the @ prefix because I don't have
to do it
for further slashes.
--
Stanimir Stoyanovhttp://stoyanoff.info



I understand that the following would split the line in words space
separated: string[] words = Regex.Split(text, @"\W+"); Why do we
need @ before regular expression?

Thanks

I see, I use @ to escape line-breaks, that seems another useful
application.

Thanks
 
Back
Top