我有以下字符串
Thx,-peter
发布于 2013-05-08 06:27:14
这就行了:(.*?)-[0-9][0-9.]+
.*?匹配任何东西,但尽可能少。这是您想要提取的捕获组。-[0-9][0-9.]匹配一个连字符,然后是一个数字,然后是任意数量的数字和句点。
发布于 2013-05-08 06:26:43
C#示例
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main(string[] args)
{
string[] terms = new string[] {
"jenkins-client-1.4",
"perl-5.16",
"ruby-1.9",
"10gen-mms-agent-1.0"
};
Regex termRegex = new Regex(@"^(.+)-(\d+[.]\d+)$");
foreach (string term in terms)
if (termRegex.IsMatch(term))
Console.WriteLine(termRegex.Match(term).Groups[1].Value);
Console.ReadLine();
}
}https://stackoverflow.com/questions/16433957
复制相似问题