简体   繁体   English

C# 中的正则表达式:在特殊字符 + 特定文本 + 直到特殊字符之后匹配

[英]Regular Expression in C# : Matching after a special character + specific text + till special character

I am looking for a pattern that matches everything after the special character } then match a text and then till first special character.我正在寻找一个匹配特殊字符}之后的所有内容的模式,然后匹配一个文本,然后直到第一个特殊字符。

this will be clear with example.my string is like这将通过 example.my 字符串很清楚

string a = @"}var e=l.visible}.abc.jYOxx{margin-top:0px}.acOxx{margin-top:0px}";

I want to extract everything after last } and text matching is ".jYOxx" and then everything before first } .我想提取最后一个}之后的所有内容,文本匹配是“.jYOxx”,然后是第一个}之前的所有内容。

result of my desired regex will be:我想要的正则表达式的结果将是:

.abc.jYOxx{margin-top:0px}

I wrote this:我写了这个:

MatchCollection matchesSource = Regex.Matches(a, @"\}(.*?).jYOxx(.*?)}", RegexOptions.Multiline);

my result is:我的结果是:

}var e=l.visible}.abc.jYOxx{margin-top:0px}

Logically I have a string of css classes and I have class name to match I want to extract that css class only.从逻辑上讲,我有一串css类,并且我有 class 名称来匹配我想仅提取ZC7A62 8CBA22E28EB17B5F5C6AE2A266AZ ZA2F2ED4F8EBC2CBBDZC21 的名称。 Please help I have wasted whole day in it.请帮助我浪费了一整天的时间。

You can match any char except the curly brackets before matching .jYOxx在匹配.jYOxx之前,您可以匹配除大括号外的任何字符

If you also do not want to match whitspace chars, you can add \s to the negated character class [^{}\s]如果您也不想匹配 whitspace 字符,您可以将\s添加到否定字符 class [^{}\s]

[^{}]*\.jYOxx{[^{}]*}

Regex demo正则表达式演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM