Solved

C# regex replace a name group with another value

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Data Analysis 7 59
Read top line from CSV file 14 60
Disable the weekends on datepicker control 6 52
Error in Visual Project 10 49
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…

752 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