In C#, how do I find all the words starting with '$' sign and ending with space, in a long string, using Regular expressions?
views:
85answers:
3Try:
var matches = Regex.Matches(input, "(\\$\\w+) ");
In the above, \\w
matches word characters. These are A-Z, a-z, - 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 e.g. [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.
var a1 = "fdjksf $jgjkd $hfj".Split(" ".ToCharArray())
.ToList()
.Where(X=>Regex.Match(X , "(\\$[a-zA-Z]*)").Success);
you may try it without regex, 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 write a simple program to benchmark, there goes resutls
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) is faster than regex functions ;)