Solved

match a pattern in c# using regex

Posted on 2014-03-17
8
521 Views
Last Modified: 2014-03-18
I want to check this pattern

start with atleast 2 digits and a hyphen
like number number, anychars [hyphen]..repeat to match below string

Page 1 of 7 280DABC2BB-5ECDC9A0-144B9203297-17F85
0
Comment
Question by:ajaypappan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 100 total points
ID: 39935469
If  you're wanting to capture the full code from the given string, this might work for you:
\d\d([A-Z\d]*-)+[A-Z\d]*

Open in new window


I've assumed "anychars" includes numbers and capital letters at this stage.
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39935472
Don't know about the "Page 1 of 27" part.

For the rest, you can use
@"\d{2}([a-zA-Z0-9]*?-)+[a-zA-Z0-9]*$"

LE: 40 seconds late :)

HTH,
Dan
0
 
LVL 3

Author Comment

by:ajaypappan
ID: 39935520
@"\d{2}([a-zA-Z0-9]*?-)+[a-zA-Z0-9]*$"

this is not capturing the page part. I want the whole thing to be capture starting from page
it can be
 Page * of *  280DABC2BB-5ECDC9A0-144B9203297-17F85

Page 1 of 7 280DABC2BB-5ECDC9A0-144B9203297-17F85
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39935525
@"Page \d+ of \d+ \d{2}([a-zA-Z0-9]*?-)+[a-zA-Z0-9]*$"
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 39935529
Page \d+ of \d+ \d\d([A-Z\d]*-)+[A-Z\d]*

Open in new window

0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39935574
It can be refined:
"Page [1-9][0-9]* of [1-9][0-9]* [1-9][0-9]([A-Za-z0-9]*-)+[A-Za-z0-9]*"

Open in new window

The page number (or number of pages) should not start with 0. Same for the serial number.
You can remove the "a-z" if you only have capital letters.
0
 
LVL 3

Author Comment

by:ajaypappan
ID: 39936985
The code is matching both strings thats wrong, it should match the first value only

 string fieldValue="280DABC2BB-5ECDC9A0-144B9203297-17F85";
 string fieldValue = "102-33-3639";

I want the whole 3 hyphen to be recognized by the pattern or it will match something else
0
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 400 total points
ID: 39937047
That was not in the constraints :) This will match only if there are 4 groups of characters, separated by "-"

"Page [1-9][0-9]* of [1-9][0-9]* [1-9][0-9]([A-Za-z0-9]*-){3}[A-Za-z0-9]*"

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question