473,320 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

RegEx Replace

I'm using code similar to the following to search a line of code. I want to
replace what was found with a modified version of the text. My regex
pattern is "ListView\w*". And the line of text that I am matching against
is: "public static void AutoSizeListView(ref ListView lv)". My RegEx finds
two matches but I want the resulting string to look like:

public static void AutoSize%TG%ListView%TG%(ref %TG%ListView%TG% lv)

As you can see - I'm replacing the text that was found with the same text
but with a "%TG%" on the front and behind the text. I'm not quite sure how
to take my match value and use it again in the replace. Can someone help me
out?

Thanks!
Brian

---------------<snip>------------------
Regex r;
r = new Regex(@"ListView\w*",
RegexOptions.IgnoreCase|System.Text.RegularExpress ions.RegexOptions.IgnorePatternWhitespace);
Match m = r.Match(@"public static void AutoSizeListView(ref ListView
lv)");
while (m.Success)
{
foreach (Group g in m.Groups)
{
foreach (Capture c in g.Captures)
{
Console.WriteLine("I found 1! " + c.Index.ToString() + " " +
c.Length.ToString() + " " + c.Value.ToString());
}
}
m = m.NextMatch();
}
Nov 16 '05 #1
1 1877
Use the key characters '$&' to refer to the 'match found string'

string text = "public static void AutoSizeListView(ref ListView lv)";
string pattern = @"ListView\w*";
text = Regex.Replace(text, pattern, "%TG%$&%TG%");
Brian Patterson wrote:
I'm using code similar to the following to search a line of code. I want to replace what was found with a modified version of the text. My regex pattern is "ListView\w*". And the line of text that I am matching against is: "public static void AutoSizeListView(ref ListView lv)". My RegEx finds two matches but I want the resulting string to look like:

public static void AutoSize%TG%ListView%TG%(ref %TG%ListView%TG% lv)

As you can see - I'm replacing the text that was found with the same text but with a "%TG%" on the front and behind the text. I'm not quite sure how to take my match value and use it again in the replace. Can someone help me out?

Thanks!
Brian

---------------<snip>------------------
Regex r;
r = new Regex(@"ListView\w*",
RegexOptions.IgnoreCase|System.Text.RegularExpress ions.RegexOptions.IgnorePatternWhitespace); Match m = r.Match(@"public static void AutoSizeListView(ref ListView lv)");
while (m.Success)
{
foreach (Group g in m.Groups)
{
foreach (Capture c in g.Captures)
{
Console.WriteLine("I found 1! " + c.Index.ToString() + " " + c.Length.ToString() + " " + c.Value.ToString());
}
}
m = m.NextMatch();
}


Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jon Maz | last post by:
Hi All, Am getting frustrated trying to port the following (pretty simple) function to CSharp. The problem is that I'm lousy at Regular Expressions.... //from...
7
by: bill tie | last post by:
I'd appreciate it if you could advise. 1. How do I replace "\" (backslash) with anything? 2. Suppose I want to replace (a) every occurrence of characters "a", "b", "c", "d" with "x", (b)...
6
by: tshad | last post by:
Is there a way to use Regex inside of a tag, such as asp:label? I tried something like this but can't make it work: <asp:label id="Phone" text=Regex.Replace('<%# Container.DataItem("Phone")...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
4
by: Cor | last post by:
Hi Newsgroup, I have given an answer in this newsgroup about a "Replace". There came an answer on that I did not understand, so I have done some tests. I got the idea that someone said,...
3
by: Craig Buchanan | last post by:
Is there a way to combine these two Replace into a single line? Regex.Replace(Subject, "\&", "&amp;") Regex.Replace(Subject, "\'", "&apos;") Perhaps Regex.Replace(Subject, "{\&|\'}", "{&amp;|&apos;}")...
9
by: Whitless | last post by:
Okay I am ready to pull what little hair I have left out. I pass the function below my String to search, my find string (a regular expression) and my replace string (another regular expression)....
4
by: Morgan Cheng | last post by:
In my case, I have to remove any line containing "0.000000" from input string. In below case, it takes about 100 ms for 2k size input string. Regex.Replace(inputString, ".*0\\.000000.*\n", ""); I...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
0
by: Karch | last post by:
I have these two methods that are chewing up a ton of CPU time in my application. Does anyone have any suggestions on how to optimize them or rewrite them without Regex? The most time-consuming...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.