tags:

views:

56

answers:

2

i am trying to parse out a string and in some cases there is an extra " - [some number]" at the end. for example,

instead of showing

 Technologist

it shows

Technologist - 23423

i dont want to just check or split on "-" because there are other names that do have a "-" in them

can anyone think of a clean way of removing this extra noise so:

Technologist - 23423 resolves to Technologist

+2  A: 

This looks like a case regular expressions, such as @" - \d+$" in this case. Sample code:

using System;
using System.Text.RegularExpressions;

class Test
{
    static void Main()
    {
        Tidy("Technologist - 12345");
        Tidy("No trailing stuff");
        Tidy("A-B1 - 1 - other things");
    }

    private static readonly Regex regex = new Regex(@"- \d+$");

    static void Tidy(string text)
    {
        string tidied = regex.Replace(text, "");
        Console.WriteLine("'{0}' => '{1}'", text, tidied);
    }
}

Note that this currently doesn't spot negative numbers. If you wanted it to, you could use

new Regex(@"- -?\d+$");
Jon Skeet
@Jon Skeet - how would i go about checking if i need to do a tidy. I guess i can call Tidy on all my records and it would just do nothing on the other ones.
ooo
@ooo: Yup, and that would be simpler than checking whether or not you needed to tidy, and then doing so only when necessary.
Jon Skeet
A: 

Try this regular expression:

var strRegex = @"\s*-\s*\d+$";

var regex = new Regex(strRegexs);

var strTargetString = "Technologist - 23423";

var res = myRegex.Replace(strTargetString, "");

This will work on the following strings (all is evaluating to Text):

Text - 34234
Text -1
Text    -     342
Text-3443
lasseespeholt