Solved

Regular Expressions in C#

Posted on 2013-11-12
2
445 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

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,…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
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…

831 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