1

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 Answers4

3

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

BasssS
  • 921
  • 1
  • 6
  • 5
  • Thanks, Your answer is correct but its not fitting into my requirement if I change my input text to "This program is written inC#We'll delete it after ten days" then it should not match since I need to match complete word only – AnsariTanveer Dec 04 '13 at 12:44
  • 1
    Thanks, Your answer is correct but its not fitting into my requirement if I change my input text to MatchCollection matches = Regex.Matches(@"This program is written in Microsoft visual basic delete it after ten days", @"\bMicrosoft\ visual\ basic\B"); – AnsariTanveer Dec 04 '13 at 12:54
1

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++
Tafari
  • 2,639
  • 4
  • 20
  • 28
0

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.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
0

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++!]
DavidRR
  • 18,291
  • 25
  • 109
  • 191