简体   繁体   English

如何在长字符串中找到所有以'$'开头并以空格结尾的单词?

[英]How to find all the words starting with '$' sign and ending with space, in a long string?

在C#中,如何使用正则表达式在长字符串中找到所有以'$'符号开头并以空格结尾的单词?

Try: 尝试:

var matches = Regex.Matches(input, "(\\$\\w+) ");

In the above, \\\\w matches word characters. 在上面, \\\\w匹配单词字符。 These are AZ, az, - and _ if I'm correct. 这些是AZ,az, - 和_如果我是正确的。 If you want to match everything that's not a space, you can use \\\\S . 如果要匹配不是空格的所有内容,可以使用\\\\S If you want a specific set, specify this through eg [a-zA-Z0-9] . 如果需要特定的设置,请通过例如[a-zA-Z0-9]

The brackets around the (\\\\$\\\\w+) ensures that of a specific match, matches[0].Groups[1].Value; (\\\\$\\\\w+)周围的括号确保特定匹配, matches[0].Groups[1].Value; gives the value inside the backets (so, excluding the trailing space). 给出了支持内部的值(因此,不包括尾随空格)。

As a complete example: 作为一个完整的例子:

string input = "$a1 $a2 $b1 $b2";

foreach (Match match in Regex.Matches(input, "(\\$\\w+) "))
{
    Console.WriteLine(match.Groups[1].Value);
}

This produces the following output: 这会产生以下输出:

$a1
$a2
$b1

The $b2 is of course omitted because it does not have a trailing space. $ b2当然被省略,因为它没有尾随空格。

You may try it without regular expressions, that may be faster. 您可以在没有正则表达式的情况下尝试它,这可能会更快。

string longText = "";
    List<string> found = new List<string>();
    foreach (var item in longText.Split(' '))
    {
        if (item.StartsWith("$"))
        {
            found.Add(item);
        }
    }

EDIT: After Zain Shaikh's comment I've written a simple program to benchmark, here goes the results. 编辑:在Zain Shaikh的评论之后,我写了一个简单的程序来进行基准测试,结果如下。

        string input = "$a1 $a2 $b1 $b2 $a2 $b1 $b2 $a2 $b1 $b2 $a2 $b1 $b2 $a2 $b1 $b2 $a2 $b1 $b2 $a2 $b1 $b2 $a2 $b1 $b2 $a2 $b1 $b2 $a2 $b1 $b2 $a2 $b1 $b2 $a2 $b1 $b2 $a2 $b1 $b2 $a2 $b1 $b2 $a2 $b1 $b2 $a2 $b1 $b2 $a2 $b1 $b2 $a2 $b1 $b2 $a2 $b1 $b2 $a2 $b1 $b2 $a2 $b1 $b2 $a2 $b1 $b2 $a2 $b1 $b2 $a2 $b1 $b2 $a2 $b1 $b2 $a2 $b1 $b2 $a2 $b1 $b2 $a2 $b1 $b2 $a2 $b1 $b2";
        var s1 = Stopwatch.StartNew();
        double first;
        foreach (Match match in Regex.Matches(input, "(\\$\\w+) "))
        {
        }
        s1.Stop();
        Console.WriteLine(" 1) " + (s1.Elapsed.TotalMilliseconds * 1000 * 1000).ToString("0.00 ns"));
        first = s1.Elapsed.TotalMilliseconds;
        s1.Reset();

        s1 = Stopwatch.StartNew();

        foreach (var item in input.Split(' '))
        {
            if (item.StartsWith("$"))
            {
            }
        }
        s1.Stop();
        Console.WriteLine(" 2) " + (s1.Elapsed.TotalMilliseconds * 1000 * 1000).ToString("0.00 ns"));
        Console.WriteLine(s1.Elapsed.TotalMilliseconds - first);

Output: 输出:

1) 730600.00 ns

2)  53000.00 ns

-0.6776

That means string functions (also with foreach) are faster than regular expression functions ;) 这意味着字符串函数(也使用foreach)比正则表达式函数更快;)

var a1 = "fdjksf $jgjkd $hfj".Split(" ".ToCharArray())
                                     .ToList()
                                     .Where(X=>Regex.Match(X , "(\\$[a-zA-Z]*)").Success);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM