473,725 Members | 1,980 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(S ubject, "\&", "&")
Regex.Replace(S ubject, "\'", "'")

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

Thanks,

Craig
Nov 20 '05 #1
3 8258
"Craig Buchanan" <so*****@micros oft.com> schrieb
Is there a way to combine these two Replace into a single line?

Regex.Replace(S ubject, "\&", "&amp;")
Regex.Replace(S ubject, "\'", "&apos;")

Perhaps Regex.Replace(S ubject, "{\&|\'}", "{&amp;|&apos;} ")

Have a look at microsoft.publi c.dotnet.framew ork
--
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(S ubject, "(\&|\"")", AddressOf Match_Craig)

Private Function Match_Craig(ByV al 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.R eplace 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(S ubject)
sb.Replace("&", "&amp;")
sb.Replace("""" , "&apos;")
Subject = sb.ToString();
Remember there are three Split functions in .NET:

Use Microsoft.Visua lBasic.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.S plit if you need to split a string based on a collection
of specific characters. Each individual character is its own delimiter.

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

Hope this helps
Jay
"Craig Buchanan" <so*****@micros oft.com> wrote in message
news:e$******** ******@tk2msftn gp13.phx.gbl...
Is there a way to combine these two Replace into a single line?

Regex.Replace(S ubject, "\&", "&amp;")
Regex.Replace(S ubject, "\'", "&apos;")

Perhaps Regex.Replace(S ubject, "{\&|\'}", "{&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
13675
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 replacements quick -> slow jump -> walk
32
14876
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 ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
4
3129
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 replace multiple entries of an object within a string. That is, if a user hit twice to create one new line, but just once to create another, I want both types ( & twice to create a new line (by creating a blank line between paragraphs). ...
4
4836
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 way mean to secrife lots of time in asp.net. can any one give me such a expression in which i pass a data string and search word string and replace word string? if so plz help me out. i'm in badly need.
5
5481
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> /*------------------------------------------------------------------------------------------- | Function name : strrep
6
2674
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! # Search & Replace file = open("text.txt", "r") text = file.read()
8
13070
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 StringBuilder. But after doing that, it seems there's no proper methods defined in StringBuilder for me to search for a specific keyword. How to resolve this problem? Does this mean I can not use StringBuilder for keyword search?
19
2455
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 2007/07/27 11:00:03 52 00 52 02 02 C7 83 D7 07 07 1B 0B 00 00 00 00 2007/07/27 11:00:03 28 0A 06 06 06 06 06 06 06 06 06 06 06 0A 06 06
4
1948
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 than one field or check muliple boxes to query the database. What I want to do is create a multiple search criteria data string. Below is the code that working with a the moment. Any input would be appreciate. Thanks Greg If...
0
8888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9174
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9111
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6702
isladogs
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4517
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3221
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
3
2157
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.