在C#中,如何使用正则表达式在长字符串中找到所有以'$'符号开头并以空格结尾的单词?
Try:
var matches = Regex.Matches(input, "(\\$\\w+) ");
In the above, \\\\w
matches word characters. These are AZ, az, - and _ if I'm correct. If you want to match everything that's not a space, you can use \\\\S
. If you want a specific set, specify this through eg [a-zA-Z0-9]
.
The brackets around the (\\\\$\\\\w+)
ensures that of a specific match, 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.
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.
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 ;)
var a1 = "fdjksf $jgjkd $hfj".Split(" ".ToCharArray())
.ToList()
.Where(X=>Regex.Match(X , "(\\$[a-zA-Z]*)").Success);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.