当不使用正则表达式在C#(或Java,C ++等) [英] When not to use Regex in C# (or Java, C++, etc.)

查看:211
本文介绍了当不使用正则表达式在C#(或Java,C ++等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有很多的问题,看起来像一个简单的regex前pression会解决,但被证明是的很辛苦与正则表达式来解决这个问题是清楚的。

It is clear that there are lots of problems that look like a simple regex expression will solve, but which prove to be very hard to solve with regex.

那么,如何有人认为是的不是专家,在正则表达式,知道他/她应该学习正则表达式来解决特定的问题?

So how does someone that is not an expert in regex, know if he/she should be learning regex to solve a given problem?

(见<一href=\"http://stackoverflow.com/questions/965667/regex-to-parse-c-source-$c$c-to-find-all-strings/968862#968862\">\"Regex解析C#源$ C ​​$ C找到办法,我问这个问题的所有字符串。)

这似乎概括起来很好:

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

(我只是改变了问题的标题,使之更加具体,如一些在C#中使用正则表达式的问题,Perl和JScript中得到解决,例如该报价的两个层次,使一个正则表达式因此不可读的事实。)

(I have just changed the title of the question to make it more specific, as some of the problems with Regex in C# are solved in Perl and JScript, for example the fact that the two levels of quoting makes a Regex so unreadable.)

推荐答案

不要试图用正则表达式解析像节目源(或嵌套XML)分级文本:他们被证明是不足够强大,例如他们不能,括号的字符串,弄清楚他们是否是平衡与否。

Don't try to use regex to parse hierarchical text like program source (or nested XML): they are proven to be not powerful enough for that, for example, they can't, for a string of parens, figure out whether they're balanced or not.

使用的解析器生成(或类似技术)为。

Use parser generators (or similar technologies) for that.

另外,我建议不要使用正则表达式来验证严格的正式的标准数据,如电子邮件地址。
他们比你想更难,你要么必须unaccurate还是一个很长的正则表达式。

Also, I'd not recommend using regex to validate data with strict formal standards, like e-mail addresses. They're harder than you want, and you'll either have unaccurate or a very long regex.

这篇关于当不使用正则表达式在C#(或Java,C ++等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆