Solved

C# regex replace a name group with another value

Posted on 2014-03-09
3
429 Views
Last Modified: 2016-06-20
I have a document of text that I use a regex to look for a specific line.  Within this specific line, there are named groups.  For one of these groups, I need to replace the value with one that is supplied by the user.  I cannot do a simple find/replace because the replacement is only applicable in the line of text matched by the regex.  I am working with EDI files.  The example I'm using below is simply an example, not a properly formatted EDI document.  The "edi" variable is my source document.

const string lastNamePattern = @"(?<NM1>NM1\*IL\*1\*(?<LastName>[a-zA-Z0-9]{0,60})\*[a-zA-Z0-9]{0,35}\*\*\*\*MI\*[a-zA-Z0-9]{2,80}~)";

const string edi = "ISA*PERRY~NM1*IL*1*PERRY*CHRISTOPHER****MI*1234567890A~IEA~";

Open in new window


I want to replace the named group "LastName" within the "NM1" group with another value.  I need to return the whole edi string with the replaced value.  Any help would be appreciated!
0
Comment
Question by:cperry-sca
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 39916557
I'm a PHP programmer, but I can say it should be pretty easy to do it like this (code generated by myregextester.com):
using System;
using System.Text.RegularExpressions;
namespace myapp
{
  class Class1
    {
      static void Main(string[] args)
        {
          String sourcestring = "source string to match with pattern";
          String matchpattern = @"(NM1\*IL\*1\*)(?<LastName>[a-zA-Z0-9]{0,60})(\*[a-zA-Z0-9]{0,35}\*\*\*\*MI\*[a-zA-Z0-9]{2,80}~)";
          String replacementpattern = @"\1NewLastName\3";
          Console.WriteLine(Regex.Replace(sourcestring,matchpattern,replacementpattern));
        }
    }
}

Open in new window

Result:
ISA*PERRY~NM1*IL*1*NewLastName*CHRISTOPHER****MI*1234567890A~IEA~

Open in new window


You should be able to play with it here: http://www.myregextester.com/?r=c9da41c5
0
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 500 total points
ID: 39916560
I've wrapped the text before the last name in their own capturing brackets, and the same for the text after the last name. The replacement pattern puts that captured text back into the result, along with the value "NewLastName" which you can change to whatever you need.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…

919 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

14 Experts available now in Live!

Get 1:1 Help Now