如何检测以数字结尾的字符串 [英] how to detect strings that end with a number
本文介绍了如何检测以数字结尾的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试解析一个字符串,在某些情况下,末尾会有一个额外的-[某些数字]".例如,
i am trying to parse out a string and in some cases there is an extra " - [some number]" at the end. for example,
而不是显示
Technologist
它显示
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:
技术人员-23423 解析为技术人员
推荐答案
在这种情况下,它看起来像一个正则表达式,例如@-\ d + $".示例代码:
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+$");
这篇关于如何检测以数字结尾的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文