tags:

views:

77

answers:

5

Here is the text I have

Remediation2009November Remediation2009December

Here is the regex I developed to find them

Remediation2009(November|December)

What I am not sure about is how to develop a regex so that when I perform the replace I can simply append a word to the end of my matches

Remediation2009NovemberCompany2 Remediation2009DecemberCompany2

Thanks

A: 

s/Remediation2009(November|September)/Remedation2009\1Company2/

Martin Kopta
A: 

In Python

import re
text="Remediation2009November Remediation2009December"
re.sub("(Remediation2009(?:November|December))","\\1Company2",text)

will do

S.Mark
+3  A: 

Since you did not mention what language - here's a C# example

public Regex MyRegex = new Regex(
      "Remediation2009(November|December)\\s+",
    RegexOptions.IgnoreCase
    | RegexOptions.CultureInvariant
    | RegexOptions.IgnorePatternWhitespace
    | RegexOptions.Compiled
    );


// This is the replacement string
public string MyRegexReplace = 
      "Remediation2009($1)Company2 ";


//// Replace the matched text in the InputText using the replacement pattern
string result = MyRegex.Replace(InputText,MyRegexReplace);

Hope this helps, Best regards, Tom.

tommieb75
Damn, I was in the middle of editing this after you mentioned Python! Apologies! :(
tommieb75
+1 no problem, it will help someone at someday, anyway, :D
S.Mark
I was in SQL Server Management Studio not Python. Sorry for the confusion. Given is the above example complete? Thanks! +1
M Murphy
+1  A: 

Here's an exmaple using C#, if you specify the language your using I could provide the solution in your specific language.

using System;
using System.Text.RegularExpressions;

namespace ConsoleApplication4
{
   class Program
   {
       static void Main(string[] args)
       {
        var input = "Remediation2009December";
        var regex = new Regex("Remediation2009(November|December)");
        var output = regex.Replace(input, "$0Company2");

        Console.WriteLine(output);
        Console.ReadLine();
      }
   }
}
Alan
A: 

why do you need a regex? In Python, its easy to do strings manipulations just by using in built string functions

>>> s="Remediation2009November Remediation2009December".split()
>>> for n,word in enumerate(s.split()):
...   if word.endswith("November") or word.endswith("December"):
...     s[n]=word+"Company2"
>>> print ' '.join(s)
Remediation2009NovemberCompany2 Remediation2009DecemberCompany2