正则表达式用于构建字符串中以“FT”开头的字符串数组

时间:2013-11-26 01:10:23

标签: c# regex arrays

我正在搜索一个名为poop的字符串,其中匹配为: “FT”后跟最多6位数字,例如FT123456 “FT”后跟任意数量的空格,最多6位数。例如FT 3435

任何事情都可以跟随比赛或在比赛之前,例如FT123456

这是我到目前为止所拥有的

string poop = "There must be something to terroir, FT1988 given that expert FT 3245 wine tasters can often identify the region from which a wine comes. But American wine growers have long expressed varying degreesFT26666 of skepticism about this ineffable concept, some dismissing it as unfathomable mysticism and others regarding it as a shrewd >FT34323</a>  marketing ploy to protect the cachet of French wines";


        Regex regex = new Regex(@"FT\d{1,6}");
        Match match = regex.Match(poop);
        if (match.Success)
        {
            return match.Value;
        }

        return "tough luck kid";

它适用于返回FT1988的第一个匹配,但不允许空格,并且不构建所有匹配的数组,这是我真正想要的。

匹配的结果应该是数组{FT1988,FT3245,FT26666,FT34323} 请注意,它会删除它在FT和以下数字之间找到的任何空格。如果它找到两个相同的值,则不应添加副本。数组应该是唯一值。

提前致谢!

2 个答案:

答案 0 :(得分:1)

使用FT\s*\d{1,6}并致电Matches()而不是Match()

这样的事情应该有效:

string poop = "There must be something to terroir, FT1988 given that expert FT 3245 wine tasters can often identify the region from which a wine comes. But American wine growers have long expressed varying degreesFT26666 of skepticism about this ineffable concept, some dismissing it as unfathomable mysticism and others regarding it as a shrewd >FT34323</a>  marketing ploy to protect the cachet of French wines";

Regex regex = new Regex(@"FT\s*\d{1,6}");
var retVal = new List<string>();
foreach (Match match in regex.Matches(poop))
    retVal.Add(match.Value.Replace(" ", ""));

return retVal.Distinct().ToList();

仔细考虑您的要求。如果在“FT”字符串之前或之后有任何内容,则正则表达式也将匹配“1234567890FT1234567890”中的“FT123456”。这可能是你期望与否。

答案 1 :(得分:0)

考虑以下Regex ......

^FT[\s\d]{1,6}$

祝你好运!