468,277 Members | 1,569 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,277 developers. It's quick & easy.

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 7548
"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Christopher R. Barry | last post: by
32 posts views Thread by tshad | last post: by
4 posts views Thread by Neo Geshel | last post: by
5 posts views Thread by int main(void) | last post: by
6 posts views Thread by DataSmash | last post: by
19 posts views Thread by =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.