RegEx Replace | Bytes (2024)

Home Posts Topics Members FAQ

Brian Patterson

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 AutoSizeListVie w(ref ListView lv)". My RegEx finds
two matches but I want the resulting string to look like:

public static void AutoSize%TG%Lis tView%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(@"ListVie w\w*",
RegexOptions.Ig noreCase|System .Text.RegularEx pressions.Regex Options.IgnoreP atternWhitespac e);
Match m = r.Match(@"publi c static void AutoSizeListVie w(ref ListView
lv)");
while (m.Success)
{
foreach (Group g in m.Groups)
{
foreach (Capture c in g.Captures)
{
Console.WriteLi ne("I found 1! " + c.Index.ToStrin g() + " " +
c.Length.ToStri ng() + " " + c.Value.ToStrin g());
}
}
m = m.NextMatch();
}

Nov 16 '05 #1

Subscribe Reply

1 RegEx Replace | Bytes (1) 1901 RegEx Replace | Bytes (2)

»· Fred ·«

Use the key characters '$&' to refer to the 'match found string'

string text = "public static void AutoSizeListVie w(ref ListView lv)";
string pattern = @"ListView\w *";
text = Regex.Replace(t ext, pattern, "%TG%$&%TG% ");
Brian Patterson wrote:

I'm using code similar to the following to search a line of code. Iwant 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 matchingagainst is: "public static void AutoSizeListVie w(ref ListView lv)". My RegExfinds two matches but I want the resulting string to look like:

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

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

Thanks!
Brian

---------------<snip>------------------
Regex r;
r = new Regex(@"ListVie w\w*",
RegexOptions.Ig noreCase|System .Text.RegularEx pressions.Regex Options.IgnoreP atternWhitespac e); Match m = r.Match(@"publi c static void AutoSizeListVie w(refListView lv)");
while (m.Success)
{
foreach (Group g in m.Groups)
{
foreach (Capture c in g.Captures)
{
Console.WriteLi ne("I found 1! " + c.Index.ToStrin g() + "" + c.Length.ToStri ng() + " " + c.Value.ToStrin g());
}
}
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 2066

Translating JavaScript function with Regex to CSharp

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 http://support.microsoft.com/default.aspx?scid=kb;EN-US;246800 function fxnParseIt() { var sInputString = 'asp and database';

.NET Framework

7 2601

any regex gurus out there?

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) every occurrence of characters "p", "q", "r", "s" with "y". Right now, I do it as follows:

C# / C Sharp

6 1767

Regex inside of a tag

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") %>',"(\d{3})(\d{3})(\d{4})","($1) $2-$3") runat="server"/> I have this inside my Repeater and want it to filter the field during bind. I can do it...

ASP.NET

17 3950

Which RegEx Testing Tool Do You Prefer?

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 http://forta.com/books/0672325667/

ASP.NET

4 3836

Replace methode, Replace Function, Stringbuilder replace, Regex Replace, Split

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, that the split method and the regex.replace method was better than the string.replace method and replace function. I did not believe that.

Visual Basic .NET

3 8245

multiple search/replacements in a single Regex.Replace?

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;}") Thanks, Craig

Visual Basic .NET

9 4240

REGEX.Replace Question

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). Why does this function replace the found reg ex. with the actual string "\t" and not a tab? (in the example below out of frustration I actually...

Visual Basic .NET

4 3139

How to make Regex.Replace faster?

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 want to optimize it, so i make a static member instance instead of using static func of Regex; static Regex filter= new Regex(".*0\\.000000.*\n",...

C# / C Sharp

15 50183

Regex to remove \t \r \n from string

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

C# / C Sharp

1726

Regex woes

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 operation by a long-shot is the regex.Replace. Basically the only purpose of it is to remove spaces between opening/closing tags and the element name....

C# / C Sharp

7638

Changing the language in Windows 10

by: Hystou |last post by:

Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...

Windows Server

1 7703

The easy way to turn off automatic updates for Windows 10/11

by: Hystou |last post by:

Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...

Windows Server

1 5523

Access Europe - Using VBA to create a class based on a table - Wed 1 May

by: isladogs |last post by:

The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...

Microsoft Access / VBA

5247

Couldn’t get equations in html when convert word .docx file to html file in C#.

by: conductexam |last post by:

I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...

C# / C Sharp

3684

Trying to create a lan-to-lan vpn between two differents networks

by: TSSRALBI |last post by:

Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...

Networking - Hardware / Configuration

3671

Windows Forms - .Net 8.0

by: adsilva |last post by:

A Windows Forms form does not have the event Unload, like VB6. What one acts like?

Visual Basic .NET

1 2136

transfer the data from one system to another through ip address

by: 6302768590 |last post by:

Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

C# / C Sharp

1 1238

How to add payments to a PHP MySQL app.

by: muto222 |last post by:

How can i add a mobile payment intergratation into php mysql website.

PHP

974

Comprehensive Guide to Website Development in Toronto: Expert Insights from BSMN Consultancy

by: bsmnconsultancy |last post by:

In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

General

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

To disable or enable advertisem*nts and analytics tracking please visit the page.

RegEx Replace | Bytes (2024)

References

Top Articles
Latest Posts
Article information

Author: Pres. Carey Rath

Last Updated:

Views: 5566

Rating: 4 / 5 (41 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Pres. Carey Rath

Birthday: 1997-03-06

Address: 14955 Ledner Trail, East Rodrickfort, NE 85127-8369

Phone: +18682428114917

Job: National Technology Representative

Hobby: Sand art, Drama, Web surfing, Cycling, Brazilian jiu-jitsu, Leather crafting, Creative writing

Introduction: My name is Pres. Carey Rath, I am a faithful, funny, vast, joyous, lively, brave, glamorous person who loves writing and wants to share my knowledge and understanding with you.