Regex.Replace help

  • Thread starter Thread starter Roger
  • Start date Start date
R

Roger

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");
 
Roger said:
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".
 
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
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top