views:

48

answers:

3

I have the following text:

txtAddressSup.Text

I want the Sup to be replaced by Cus while the whole text remains as is. I have many texts that are in this format:

xxxxxxxSup.xxxxx
+1  A: 

why do you need to use regex? Won't a simple string replace work?

myString = myString.Replace("Sup.","Cus.");
Sam Holder
I need the regexp to run it in find and replace option for editing large block of code.
peace
in the ide? or can you just open the code file in a program, read the whole file in as a string, do the replace and then write the resultant string out as the new file...
Sam Holder
+1  A: 

The regex to match those would be (common Regex syntax, not the one from Visual Studio):

\BSup(?=\.\w+)

Just replace the matches with Cus

Lucero
I'm using this regexp in the Find and Replace option, which helps me make a massive editing.
peace
In this case you should rather use the "Rename..." refactoring, because search-replace usually doesn't really work as expected (matching more than it should etc.).
Lucero
+2  A: 

It's not clear from your question but I guess you are doing this in Visual Studio. Visual Studio uses a strange brand of regular expressions that is incompatible with most other engines. Try this:

Find: {:i}Sup{\.:i}
Replace: \1Cus\2

Explanation:

{...} Tag expression (usually called a capturing group in other engines)
:i    Identifier
Sup   Literal string "Sup"
\.    Literal string "."

To get help with this either see the description of the syntax for Visual Studio regular expressions on MSDN or press the black triangle next to the input fields to get quick help.

Mark Byers
+1 for visual studio answer.
Sam Holder
Thanks. This is what i was looking for.
peace