Solved

C# regex replace a name group with another value

Posted on 2014-03-09
3
373 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…

757 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

23 Experts available now in Live!

Get 1:1 Help Now