正则表达式在线匹配,但不在C#代码中(Regex matches online but not in C# code)

编程入门 行业动态 更新时间:2024-10-10 11:30:01
正则表达式在线匹配,但不在C#代码中(Regex matches online but not in C# code)

这个正则表达式适用于在线正则表达式测试人员,但是当我尝试这段代码时,正则表达式不匹配。

Regex regex = new Regex(@"^\.lnr-(.*)\:before \{$", RegexOptions.Compiled | RegexOptions.Multiline); string css = System.IO.File.ReadAllText(Server.MapPath("/linearicons-free.css")); foreach (Match match in regex.Matches(css)) { //doing sth }

我错过了什么?

This regex works at online regex testers but when I try this piece of code regex does not match.

Regex regex = new Regex(@"^\.lnr-(.*)\:before \{$", RegexOptions.Compiled | RegexOptions.Multiline); string css = System.IO.File.ReadAllText(Server.MapPath("/linearicons-free.css")); foreach (Match match in regex.Matches(css)) { //doing sth }

What am I missing ?

最满意答案

当C#解析字符串时,最后会有一个\n 。 所以你的模式^\.lnr-(.*):before \{$失败^\.lnr-(.*):before \{$因为你期望的最后一个charcater是{

将模式更改为^\.lnr-(.*):before \{\s$ ,它应该可以工作。

你可以在这里测试.NET正则表达式而不是regex101.com

There is a \n at the end when C# is parsing the string. So your pattern^\.lnr-(.*):before \{$ fails because the last charcater you are expecting is {

Change the pattern to ^\.lnr-(.*):before \{\s$ and it should work.

you can test the .NET regexes here instead of regex101.com

更多推荐

本文发布于:2023-04-29 09:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335617.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:在线   代码   正则表达式   Regex   online

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!