开发者

how to detect strings that end with a number

开发者 https://www.devze.com 2023-01-19 03:42 出处:网络
i am trying to parse out a string and in some cases there is an extra \" - [some number]\" at the end.for example,

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


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+$");


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
0

精彩评论

暂无评论...
验证码 换一张
取 消