?
Solved

match a pattern in c# using regex

Posted on 2014-03-17
8
Medium Priority
?
525 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 400 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
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!

 
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 1600 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

801 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