开发者

A RegEx for Expressions starting with $

开发者 https://www.devze.com 2023-01-02 01:45 出处:网络
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 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开发者_StackOverflow中文版 great if someone can point me in correct direction.


Try this regex:

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

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

(?<=\$)\w+(?=\$)


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: \$(.+?)\$

0

精彩评论

暂无评论...
验证码 换一张
取 消