Extremely Easy Regex Replace question
Here is the text I have
Remediation2009November Remediation2009December
Here is the regex I developed开发者_StackOverflow 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
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.
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();
}
}
}
s/Remediation2009(November|September)/Remedation2009\1Company2/
In Python
import re
text="Remediation2009November Remediation2009December"
re.sub("(Remediation2009(?:November|December))","\\1Company2",text)
will do
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
精彩评论