Solved

Regular Expressions in C#

Posted on 2013-11-12
2
439 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
2 Comments
 
LVL 9

Accepted Solution

by:
Derek Jensen earned 500 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
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…

708 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

17 Experts available now in Live!

Get 1:1 Help Now