?
Solved

Regular Expressions in C#

Posted on 2013-11-12
2
Medium Priority
?
456 Views
Last Modified: 2013-11-12
I have the following string:

DMG*D8*19481101*M~NM1*PR*2*EDS/MEDICAID*****PI*DNC00~

I need to find certain matches of that string.  Here is what I have tried:

string sPattern = @"DMG\*D8%~NM1\*PR\*2\*%PI\*DNC00~";

The issue is that, for example, the 19481101 could be a different number.  I don't care about that, nor do I care about the EDS/MEDICAID.  Those parts can change and it's fine.  In my pattern above, the parts I don't care about are denoted by a % (I realize its not legal in C# I'm just using it for illustrative purposes).  What I'm attempting to do is match up the parts I have that are literal and tell Regex that the parts denoted by % can by anything, I don't care, just ignore them.  By the way, this is all inside a text file (I'm looking for one part out of possible thousands of characters).  

Suggestions?

JamesNT
0
Comment
Question by:JamesNT
[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
2 Comments
 
LVL 9

Accepted Solution

by:
Derek Jensen earned 2000 total points
ID: 39642386
Dealing with the string

DMG\*D8%~NM1\*PR\*2\*%PI\*DNC00~
where you want the %'s to match anything, you simply replace % with .*?

string sPattern = @"DMG\*D8.*?~NM1\*PR\*2\*.*?PI\*DNC00~";

Open in new window

0
 

Author Comment

by:JamesNT
ID: 39642674
So that's all there was to it!  Once I went back and read the MSDN docs, it made perfect sense.  I guess I just needed to see it one time.

JamesNT
0

Featured Post

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!

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

764 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