Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have various strings that may or may not contain '(_#)' where '#' is a number (can be multiple digits also.

Note sometimes it does not exist (the whole sequence), but if it does it is always at the beginning. I want to remove it for display purposes if it exists, so I thought RegEx would do this easily (well I know it does), but I am quite rusty.

Here are some string examples

(_0) Welcome
(_1) Information
(_2) Products
(_3) Help
Contact Info
Maps
Celery
Wild Wings
Deep Fried Pickles
(_4) Your thinking about BWW now


So all of these strings end up in some variable s and I want to rip out the '(_#)' if it exists and store the string in display.

Thank you in advance.
Posted

1 solution

Try:
public static Regex regex = new Regex(
      @"^\(_\d*\)\s*",
    RegexOptions.IgnoreCase
    | RegexOptions.Multiline
    | RegexOptions.CultureInvariant
    | RegexOptions.IgnorePatternWhitespace
    | RegexOptions.Compiled
    );

string result = regex.Replace(InputText,"");


Get a copy of Expresso [^] - it's free, and it examines and generates Regular expressions. It generated the code once I had tested the regex!
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 7-Jul-11 9:47am    
I hope you don't mind that I opted for the literal string notation. I feel it's easier on the eyes. :)
Have my 5!
OriginalGriff 7-Jul-11 9:50am    
I agree - but Expresso always uses the long form, so I left it that way so as not to introduce a mistake.
[no name] 7-Jul-11 13:02pm    
Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900