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

Regex.Replace help

I'm having a little trouble using the Regex.Replace method. I am
trying to perform a search and replace based on string pattern. I've
been successful in replacing string from it's starting position to the
end of the string value, however, I want to better define an ending
position. Below follows the code I'm using:

string a = "Hello, World, How are you?";
textBox1.Text = Regex.Replace(a, "W.*", "Mickey");

Mar 20 '06 #1
3 1882
Roger <ro************@gmail.com> wrote:
I'm having a little trouble using the Regex.Replace method. I am
trying to perform a search and replace based on string pattern. I've
been successful in replacing string from it's starting position to the
end of the string value, however, I want to better define an ending
position. Below follows the code I'm using:

string a = "Hello, World, How are you?";
textBox1.Text = Regex.Replace(a, "W.*", "Mickey");


Well, what are you trying to achieve? My guess is that you want to
replace everything from "W" onwards *while you've got word characters*
in which case you're after the character class \w:

textBox1.Text = Regex.Replace(a, @"W\w*", "Mickey");

However, you need to be careful about exactly what your requirements
are - consider the different types of letters, as well as digits etc.
Have a look for "regular expressions" in the MSDN index and look under
"character classes".

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 20 '06 #2
Bob
Hi Roger,
I too had the Regex blues.
Found a utility on the net called RegexBuddy that lets you construct and
trial your regex on various strings or files. Costs unforunately, but worth
it IMO.
I was trying to fish a disjointed number out of a string, any thought that I
knew what I was doing went right out the door when I saw the finished
Expression.
(No I am not in anyway involved with RegexBuddy except as a customer)

Bob

"Roger" <ro************@gmail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
I'm having a little trouble using the Regex.Replace method. I am
trying to perform a search and replace based on string pattern. I've
been successful in replacing string from it's starting position to the
end of the string value, however, I want to better define an ending
position. Below follows the code I'm using:

string a = "Hello, World, How are you?";
textBox1.Text = Regex.Replace(a, "W.*", "Mickey");

Mar 21 '06 #3
I've used RegexBuddy, as well as several others, and RegexBuddy is my
favorite. In addition, I might mention that the cost of RegexBuddy is around
$30.00, so it's well worth the money. One of the best aspects of it is the
ability to create and/or analyze a Regular Expression in a visual manner. It
also has a very nice library utility, GREP utilities, and several other
really nice features. The only thing I would like to see improved is the
ability to design Regular Expressions with line breaks to separate the parts
in the text designer.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.

"Bob" <bo*@nowhere.com> wrote in message
news:OC**************@tk2msftngp13.phx.gbl...
Hi Roger,
I too had the Regex blues.
Found a utility on the net called RegexBuddy that lets you construct and
trial your regex on various strings or files. Costs unforunately, but
worth
it IMO.
I was trying to fish a disjointed number out of a string, any thought that
I
knew what I was doing went right out the door when I saw the finished
Expression.
(No I am not in anyway involved with RegexBuddy except as a customer)

Bob

"Roger" <ro************@gmail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
I'm having a little trouble using the Regex.Replace method. I am
trying to perform a search and replace based on string pattern. I've
been successful in replacing string from it's starting position to the
end of the string value, however, I want to better define an ending
position. Below follows the code I'm using:

string a = "Hello, World, How are you?";
textBox1.Text = Regex.Replace(a, "W.*", "Mickey");


Mar 21 '06 #4

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

Similar topics

3
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...
6
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")...
17
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 ...
4
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,...
2
by: Art | last post by:
Hi & help, I'm trying to parse arithmetic expressions such as 4*(7-2.1). The first thing I'm trying is to add spaces to get: 4 * ( 7 - 2.1 ). I thought that Regular Expressions would be the...
2
by: Alex Maghen | last post by:
This is a bit of an abuse of this group. Just a nit, but I'm hoping someone really good with Regular Expressions can help me out here. I need to write a regular expression that will do the...
6
by: Gary Bond | last post by:
Hi All, Being a bit of a newbie with regex, I am confused when using word boundaries. For instance, I want to replace all the stand alone '.5k' that occur in an input string, with 500. In other...
15
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
1
by: jonnyboy6969 | last post by:
Hi All Really hoping someone can help me out here with my deficient regex skills :) I have a function which takes a string of HTML and replaces a term (word or phrase) with a link. The pupose...
0
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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
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
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,...

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.