Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# regex replace a name group with another value

Posted on 2014-03-09
3
Medium Priority
?
893 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 2000 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 2000 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…

705 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