正则表达式解析C#源$ C ​​$ C找到的所有字符串字符串、正则表达式

2023-09-02 01:42:23 作者:呔痛ぺ涐承涭Ъú住

我问这个问题很久以前,   我希望我读答案   的当不使用正则表达式在C#(或   JAVA,C ++等)的 第一!

I asked this question a long time ago, I wish I had read the answers to When not to use Regex in C# (or Java, C++ etc) first!

我想使用正则表达式(普通前pressions)来获取所有字符串列表在我的C#源$ C ​​$ C,包括嵌入了双引号的字符串。

I wish to use Regex (regular expressions) to get a list of all strings in my C# source code, including strings that have double quotes embedded in them.

这应该不难,不过之前我花时间试图建立正则表达式EX pression了,有没有人得到了一个pre罐头一了吗?

This should not be hard, however before I spend time trying to build the Regex expression up, has anyone got a "pre canned" one already?

这是不是因为它似乎首先是作为易因

This is not as easy as it seems as first due to

在AV D @AB,CD @AB @AB 等

推荐答案

我张贴这是我的答案,所以它代表了其他阅读问题。

I am posting this as my answer so it stands out to other reading the questions.

正如已经指出的那样有用的意见,以我的问题,很显然,正则表达式是不是查找字符串在C#code的好工具。我可以写我花了提醒我的正则表达式语法的自我的时候一个简单的解析器。 - (Parser是一个在声明中,因为在评论等没有,这是我的源$ C ​​$ C我处理)

As has been pointed out in the helpful comments to my question, it is clear that regex is not a good tool for finding strings in C# code. I could have written a simple "parser" in the time I spent reminding my self of the regex syntax. – (Parser is a over statement as there are no " in comments etc, it is my source code I am dealing with.)

这似乎概括起来很好:

一些人,当遇到一个问题,认为我知道,我将使用   定期EX pressions。现在他们有两个问题。

不过,直到它打破我的code,我将使用常规的前pression Blixt已经发布,但如果它给我的问题,我不会花比赛时间试图写我自己的解析器之前修复它。例如作为一个C#字符串是

However until it breaks on my code I will use the regular expression Blixt has posted, but if it give me problems I will not spend match time trying to fix it before writing my own parser. E.g as a C# string it is

@"@Q(?:[^Q]+|QQ)*Q|Q(?:[^Q\]+|\.)*Q".Replace('Q', '"')

更新,上述正则表达式有问题,所以我只是写我自己的解析器,其中包括编写单元测试花了大约2个小时写的解析器。这就是我少很多的时间,然后我花只是想找到(和测试)一pre-罐头正则表达式在网络上。

Update, the above regEx had problem, so I just wrote my own parser, including writing unit tests it took about 2 hours to write the parser. That's I lot less time then I spend just trying to find (and test) a pre-canned Regex on the web.

我看有问题,是我倾向于避免正则表达式,只是c写我自己的字符串处理$ C $,然后有很多人声称,我不使用正则表达式浪费客户的钱。不过,每当我尝试使用正则表达式是什么似乎是一个简单的匹配模式成为比赛更难迅速。 (无在线的文章在.NET中使用正则表达式,我已阅读,有一个良好的指令说清楚的​​时候不要使用正则表达式,同样与它的MSDN文档)

The problem I see to have, is I tend to avoid Regex and just write the string handling code my self, then have a lot of people claim I am wasting the client’s money by not using Regex. However whenever I try to use Regex what seems like a simple match pattern becomes match harder quickly. (None the on-line articles on using Regex in .net that I have read, have a good instruction that make it clear when NOT to use Regex. Likewise with it’s MSDN documentation)

让我们来看看,如果我们可以帮助解决这个问题,我刚才已经创建了一个堆栈溢出问题当不使用正则表达式

Lets see if we can help solve this problem, I have just created a stack overflow questions "When not to use Regex"