473,394 Members | 1,746 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,394 software developers and data experts.

Regex - dynamically creating pattern issue

I'm trying to use Regex.Replace to replace a single word in a string,
but I have to pass in the string. It won't ever match, but if I hard-
code the word it works fine. Please help!!

string stringToAlter = "ab cd ef gh";

// --- Pattern entered as string works fine
string pat1 = @"\bcd\b";
string newStg1 = Regex.Replace(stringToAlter, @pat1, "xxx");
WriteLine("first try = " + newStg1);

// --- But passing in the word and concatenating doesn't!
string pat2 = @"\b" + "cd" + "\b";
newStg1 = Regex.Replace(stringToAlter, @pat2, "xxx");
WriteLine("second try = " + newStg1);

results:
first try = ab xxx ef gh
second try = ab cd ef gh

Mar 24 '07 #1
3 4548
On Mar 23, 8:54 pm, "Deanna" <ddelapa...@gmail.comwrote:
I'm trying to use Regex.Replace to replace a single word in a string,
but I have to pass in the string. It won't ever match, but if I hard-
code the word it works fine. Please help!!

string stringToAlter = "ab cd ef gh";

// --- Pattern entered as string works fine
string pat1 = @"\bcd\b";
string newStg1 = Regex.Replace(stringToAlter, @pat1, "xxx");
WriteLine("first try = " + newStg1);

// --- But passing in the word and concatenating doesn't!
string pat2 = @"\b" + "cd" + "\b";
newStg1 = Regex.Replace(stringToAlter, @pat2, "xxx");
WriteLine("second try = " + newStg1);

results:
first try = ab xxx ef gh
second try = ab cd ef gh
You neglected to add the '@' to the second "\b" in your pattern. It
should be...
string pat2 = @"\b" + "cd" + @"\b";

Also, I could be wrong, but isn't the '@' unnecessary when accessing
the variable?
newStg1 = Regex.Replace(stringToAlter, @pat2, "xxx");
Mar 24 '07 #2
Deanna wrote:
I'm trying to use Regex.Replace to replace a single word in a string,
but I have to pass in the string. It won't ever match, but if I hard-
code the word it works fine. Please help!!

string stringToAlter = "ab cd ef gh";

// --- Pattern entered as string works fine
string pat1 = @"\bcd\b";
string newStg1 = Regex.Replace(stringToAlter, @pat1, "xxx");
WriteLine("first try = " + newStg1);

// --- But passing in the word and concatenating doesn't!
string pat2 = @"\b" + "cd" + "\b";
Change "\b" to @"\b".
newStg1 = Regex.Replace(stringToAlter, @pat2, "xxx");
WriteLine("second try = " + newStg1);

results:
first try = ab xxx ef gh
second try = ab cd ef gh
If the string might contain characters that have any special meaning in
a regular expression, use the Regex.Escape method on when creating the
pattern.

--
Göran Andersson
_____
http://www.guffa.com
Mar 24 '07 #3
Yota wrote:
Also, I could be wrong, but isn't the '@' unnecessary when accessing
the variable?
>newStg1 = Regex.Replace(stringToAlter, @pat2, "xxx");
Correct. It can be used if you need to use a keyword as variable name,
like @class, but I have yet to see any situation where this really would
be needed.

--
Göran Andersson
_____
http://www.guffa.com
Mar 24 '07 #4

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

Similar topics

1
by: Colin Reid | last post by:
Hey MS, here's an apparent problem in the base class library. I pulled the email validation pattern "^((*)*@(()+(*)*\.)+{2,9})" from http://regexlib.com. If I validate the email address...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
7
by: alphatan | last post by:
Is there relative source or document for this purpose? I've searched the index of "Mastering Regular Expression", but cannot get the useful information for C. Thanks in advanced. -- Learning...
20
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex...
7
by: lgbjr | last post by:
Hi All, I'm trying to split a string on every character. The string happens to be a representation of a hex number. So, my regex expression is (). Seems simple, but for some reason, I'm not...
9
by: Greg Collins [Microsoft MVP] | last post by:
I've done a bit of research on this topic in the newsgroups and on MSDN, and though it sounds possible, I still don't understand how I would make it work. I want to be able to use named groups to...
10
by: igor.kulkin | last post by:
I have a small utility program written in Python which works pretty slow so I've decided to implement it in C. I did some benchmarking of Python's code performance. One of the parts of the program...
16
by: Mark Chambers | last post by:
Hi there, I'm seeking opinions on the use of regular expression searching. Is there general consensus on whether it's now a best practice to rely on this rather than rolling your own (string)...
7
by: Nightcrawler | last post by:
Hi all, I am trying to use regular expressions to parse out mp3 titles into three different groups (artist, title and remix). I currently have three ways to name a mp3 file: Artist - Title ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
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...

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.