473,499 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

multiple search/replacements in a single Regex.Replace?

Is there a way to combine these two Replace into a single line?

Regex.Replace(Subject, "\&", "&")
Regex.Replace(Subject, "\'", "'")

Perhaps Regex.Replace(Subject, "{\&|\'}", "{&|'}")

Thanks,

Craig
Nov 20 '05 #1
3 8230
"Craig Buchanan" <so*****@microsoft.com> schrieb
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;}")

Have a look at microsoft.public.dotnet.framework
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #2
Hi Craig

Depends what you want to do, however using the Regex for this cost you
probably 100 times more time than doing a normal *string* replace.

So doing 2 times with that string replace will save you a lot of time

Cor
Nov 20 '05 #3
Craig,
You can use a MatchEvaluator (a delegate) to call a routine for each match
you find, then you can simply call Regex.Replace with your pattern & the
AddressOf your routine. The routine will take the inner stuff found & return
the new value.

Something like:

Regex.Replace(Subject, "(\&|\"")", AddressOf Match_Craig)

Private Function Match_Craig(ByVal m As Match) As String
Select Case m.Groups[0].Value
case "&"
return "&amp;"
case """"
return "&apos;"
End Select
End Function

However I don't think I would use a RegEx in this case, I think I would use
StringBuilder.Replace in this case as you are replacing based on fixed
words, not on patterns. RegEx.Replace are better are replacing based on
patterns. ("\&" is a fixed pattern, unlike "9*" which is a variable
pattern).

Dim sb As New StringBuilder(Subject)
sb.Replace("&", "&amp;")
sb.Replace("""", "&apos;")
Subject = sb.ToString();
Remember there are three Split functions in .NET:

Use Microsoft.VisualBasic.Strings.Split if you need to split a string based
on a specific word (string). It is the Split function from VB6.

Use System.String.Split if you need to split a string based on a collection
of specific characters. Each individual character is its own delimiter.

Use System.Text.RegularExpressions.RegEx.Split to split based
on matching patterns.

Hope this helps
Jay
"Craig Buchanan" <so*****@microsoft.com> wrote in message
news:e$**************@tk2msftngp13.phx.gbl...
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

Nov 20 '05 #4

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

Similar topics

3
13654
by: Christopher R. Barry | last post by:
I need to search and replace multiple words in one pass of an input stream or string. For example, given the input: "The quick brown fox jumped over the lazy dog's back" and given the...
32
14760
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
4
3112
by: Neo Geshel | last post by:
Greetings I am using VB in my ASP.NET project that uses an admin web site to populate a database that provides content for a front end web site. I am looking for a way to use replace() to...
4
4820
by: lucky | last post by:
hi there!! i'm looking for a code snipett wich help me to search some words into a particular string and replace with a perticular word. i got a huge data string in which searching traditional...
5
5460
by: int main(void) | last post by:
Hi all, Following is my attempt to write a string search and replace function. #include <stdio.h> #include <stdlib.h> #include <string.h>...
6
2657
by: DataSmash | last post by:
Hello, I need to search and replace 4 words in a text file. Below is my attempt at it, but this code appends a copy of the text file within itself 4 times. Can someone help me out. Thanks! #...
8
13046
by: Laser Lu | last post by:
Sometimes, I need to do some time-consuming operations based on whether a specific keyword was contained in a lengthy string. Then, for a better performance, I wrapped that lengthy string into a...
19
2432
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post by:
This is an example of the data; 2007/07/27 11:00:03 ARES_INDICATION 010.050.016.002 404.2.01 (6511) RX 74 bytes 2007/07/27 11:00:03 65 11 26 02 BC 6C AA 20 76 93 51 53 50 76 13 48...
4
1932
by: gblack301 | last post by:
Hi, I have a search form where the user can check a box or enter some data such as a name to quey the database. I was wondering what is the best way to enable the ability for a user data in more...
0
7012
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...
0
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6901
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...
0
7392
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5479
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4605
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...
0
1429
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 ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
307
bsmnconsultancy
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...

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.