Solved

match a pattern in c# using regex

Posted on 2014-03-17
8
513 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
  • 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 34

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
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39935525
@"Page \d+ of \d+ \d{2}([a-zA-Z0-9]*?-)+[a-zA-Z0-9]*$"
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 34

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 34

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now