tags:

views:

47

answers:

2

I have a Expression ($ASMLNA$ * $TSM$ * 8 * ($GrossDownTarget$ * $005930K$)+15)

Now I am trying to get all the variables which is between $ $. Example $ASMLNA$ so for me it should give ASMLNA.

I have tried using RegEx and this is what I have been able to do till now

  Regex r = new Regex(@"[^\$]");

        string Contents = txtRegEx.Text.Trim();
        MatchCollection ImageCollection = r.Matches(Contents);
        string tempContents = string.Empty;
        foreach (Match match in ImageCollection)
        {
            tempContents+= match.Value;
        }

It will be great if someone can point me in correct direction.

+3  A: 

Try this regex:

(?<=\$)\b[^$]+\b(?=\$)

If your variables can only contain word chars ([a-zA-Z0-9_]), this regex would be better:

(?<=\$)\w+(?=\$)
Senseful
This will also return the strings between - for example - the second and third `$`, will it not?
exhuma
@exhuma: I believe you are right, I updated it with `\b` which seems to be valid given the example data above. Thanks.
Senseful
A: 

Your expression only matches a $ at the beginning of the string. To get groups, I think you want something like this: @"(\$.+?\$)"

Edit: Oops. I missed the bit about stripping out the $. Try this version instead: \$(.+?)\$

ThatBlairGuy