Solved

C# regex replace a name group with another value

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…

623 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