Regex not working having reserved character in search text c#

220 views Asked by At

I have a regular expression which was working for all my requirement until now, suddenly I got a string which has reserved character like + in c++ and # in C#. Below code work for all my word collection except for c++ and C#

MatchCollection matches= Regex.Matches(@"This  program is written in C# We'll delete it after ten days", @"\bC\+\+\b");
foreach(Match m in matches)
{
      Console.Write(m.Value);
}

Can any one point out why?

4

There are 4 answers

2
BasssS On

You should use \B on the 2nd boundary instead of \b

MatchCollection matches= Regex.Matches(@"This  program is written in C# We'll delete it after ten days", @"\bC\#\B");

You can read the following link for more info : http://www.regular-expressions.info/wordboundaries.html

0
Mike Perrenoud On

Below code work for all my word collection except for c++ and C#

For that match to work you'd need a Regex like this @"(?:C\+\+)|(?:C#)" and here is a Regex 101 to prove it.

0
DavidRR On

In your situation, rather than looking for a word boundary (\b) or a non-word boundary (\B), you might instead consider looking for whitespace (\s+), beginning of the line (^), and end of the line ($).

Here's a regex that will do that:

(?:^|\s+)(C#|C\+\+)(?=\s+|$)

And here's a Perl program that demonstrates that regex on a sample data set. (Also see the live demo.)

#!/usr/bin/perl -w

use strict;
use warnings;

while (<DATA>) {
    chomp;

#   A - Preceded by the beginning of the line or 1 or more whitespace
#       characters
#   B - The character sequences 'C#' or 'C++'
#   C - Followed by 1 or more whitespace characters or the end of line.

    if (/(?:^|\s+)(C#|C\+\+)(?=\s+|$)/) {
#           ^^^^^  ^^^^^^^^    ^^^^^
#             A        B         C

        print "[$1] [$_]\n";
    } else {
        print "[--] [$_]\n";
    }
}

__END__
This program is written in C++ We'll delete it after ten days
This program is written in !C++ We'll delete it after ten days
This program is written in C++! We'll delete it after ten days
This program is written in C# We'll delete it after ten days
C# is the language this program is written in.
 C# is the language this program is written in.
C++ is the language this program is written in.
This program is written in C#
This program is written in C++
This program is written in C++!

Expected Output:

[C++] [This program is written in C++ We'll delete it after ten days]
[--] [This program is written in !C++ We'll delete it after ten days]
[--] [This program is written in C++! We'll delete it after ten days]
[C#] [This program is written in C# We'll delete it after ten days]
[C#] [C# is the language this program is written in.]
[C#] [ C# is the language this program is written in.]
[C++] [C++ is the language this program is written in.]
[C#] [This program is written in C#]
[C++] [This program is written in C++]
[--] [This program is written in C++!]
0
Tafari On

You could use following pattern, which store match in the group 1:

PATTERN

\bC(\+\+|\#)\s

And this C# code:

CODE

MatchCollection matches= Regex.Matches(@"This  program is written in C# We'll delete it after ten days", @"\bC\+\+\b");

foreach(Match m in matches)
{
     Console.Write(m.Groups[1].Value);
}

INPUT

This  program is written in C# We'll delete it after ten days

OUTPUT

C#

And

INPUT

This  program is written in C++ We'll delete it after ten days

OUTPUT

C++