views:

85

answers:

3

In C#, how do I find all the words starting with '$' sign and ending with space, in a long string, using Regular expressions?

+4  A: 

Try:

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.

Pieter
thanks for your quick reply, but this only returns me the last match from a string, I want to get all the matches.
Zain Shaikh
I've expended the answer with an example. I don't understand why this would not return all items in your input test. Could you please provide an example input which only returns the last match?
Pieter
Oh Sorry, your regex is working perfectly all right, actually I was not looping through each match, I was using your code matches[0].Groups[1].Value; and I was getting only one match. see the following code, it is working properly and returns all matches. adding code in next comment.
Zain Shaikh
String templateHtml = "Your homepage $OldHomepageUrl has been changed to $NewHomepageUrl and you can now share it with your friends."; var matches = Regex.Matches(templateHtml, "(\\$\\w+) "); for (int i = 0; i < matches.Count; i++) { var match = matches[i].Groups[0].Value; Console.WriteLine(match); }
Zain Shaikh
No problem. Though about accepting the answer?
Pieter
:) accepted. thanks!
Zain Shaikh
A: 
var a1 = "fdjksf $jgjkd $hfj".Split(" ".ToCharArray())
                                     .ToList()
                                     .Where(X=>Regex.Match(X , "(\\$[a-zA-Z]*)").Success);
Pramodh
thanks for your quick reply as well, but this only returns me the last match from a string, I want to get all the matches.
Zain Shaikh
Edited in......
Pramodh
+1  A: 

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 ;)

Ahmet Kakıcı
@Ahmet, I think you have not worked on performance optimization, the split method and foreach loop itslef are performance critical, therefore I always try to avoid them, and that is why I asked for regular expression in my question.
Zain Shaikh
Please check the 'edit' part of my answer.
Ahmet Kakıcı
hmm, good example, I am impressed, I once had to work with Split method and foreach loop, they both were performance critical, so we user alternatives.
Zain Shaikh
numbers don't lie ;)
Ahmet Kakıcı