Regular Expressions + Including one space in pattern

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"

Answers


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.


Need Your Help

Why is this crashing…?

iphone ios memory-management uinavigationcontroller

I am pushing a viewController on the navigation stack and when I reach the next view and come back and then relaunch the view it ends in a crash...

IF condition inside the repeater control?

c# javascript asp.net repeater

<asp:CheckBox ID="chkBox1" runat="server" Text="1" />

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.