4

I'm trying to figure out how to write a pattern to match to the following: "3Z 5Z". The numbers in this can vary, but the Z's are constant. The issue I'm having is trying to include the white space... Currently I have this as my pattern

 pattern = @"\b*Z\s*Z\b";

The '*' represent the wildcard for the number preceding the "Z", but it doesn't seem to want to work with the space in it. For example, I can use the following pattern successfully for matching to the same thing without the space (i.e. 3Z5Z)

pattern = @"\b*Z*Z\b";

I am writing this program in .NET 4.0 (C#). Any help is much appreciated!

EDIT: This pattern is part of a larger string, for example: 3Z 10Z lock 425"

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
keynesiancross
  • 3,441
  • 15
  • 47
  • 87

3 Answers3

5

Try this:

pattern = @"\b\d+Z\s+\d+Z\b";

Explanation:

"
\b    # Assert position at a word boundary
\d    # Match a single digit 0..9
   +     # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
Z     # Match the character “Z” literally
\s    # Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
   +     # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\d    # Match a single digit 0..9
   +     # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
Z     # Match the character “Z” literally
\b    # Assert position at a word boundary
"

By the way:

\b*

Should throw an exception. \b is a word anchor. You can't quantify it.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
FailedDev
  • 26,680
  • 9
  • 53
  • 73
  • Hi FailedDev - thanks for the suggestion but it's still not returning a Match.. Maybe you can spot a mistake - I now have: pattern = @"\b\d+Z\s+\d+Z\b"; (edit - removed astrix but still does not work) – keynesiancross Dec 01 '11 at 19:00
  • @keynesiancross **\d+*** this should also throw an exception. Remove the asterisk after the +. – FailedDev Dec 01 '11 at 19:02
  • Sorry - removed that astrix (see edit above) but still does not provide match... Also added edit to OP – keynesiancross Dec 01 '11 at 19:06
  • Turns out I'm just an idiot and was setting my breakpoint before the method runs.. You're code above works great. Thanks a lot – keynesiancross Dec 01 '11 at 19:11
1

Try this code.

using System;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      string txt="3Z 5Z";

      string re1="(\\d+)";  // Integer Number 1
      string re2="(Z)"; // Any Single Character 1
      string re3="( )"; // Any Single Character 2
      string re4="(\\d+)";  // Integer Number 2
      string re5="(Z)"; // Any Single Character 3

      Regex r = new Regex(re1+re2+re3+re4+re5,RegexOptions.IgnoreCase|RegexOptions.Singleline);
      Match m = r.Match(txt);
      if (m.Success)
      {
            String int1=m.Groups[1].ToString();
            String c1=m.Groups[2].ToString();
            String c2=m.Groups[3].ToString();
            String int2=m.Groups[4].ToString();
            String c3=m.Groups[5].ToString();
            Console.Write("("+int1.ToString()+")"+"("+c1.ToString()+")"+"("+c2.ToString()+")"+"("+int2.ToString()+")"+"("+c3.ToString()+")"+"\n");
      }
      Console.ReadLine();
    }
  }
}
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
1

I addition to other posts I would add characters of the Begin and End of string.

patter = "^\d+Z\s\d+Z$"
Uriil
  • 11,948
  • 11
  • 47
  • 68