使用或不使用正则表达式? [英] To use or not to use regular expressions?

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

问题描述

我只是问了这个问题关于使用正则表达式允许 -90.0 和 +90.0 之间的数字.我得到了一些关于如何实现正则表达式的答案,但大多数答案还提到,如果不使用正则表达式会更好地处理,或者使用正则表达式会过大.那么你如何决定何时使用正则表达式,何时不使用正则表达式.是否有您可以遵循的检查清单?

I just asked this question about using a regular expression to allow numbers between -90.0 and +90.0. I got some answers on how to implement the regular expression, but most of the answers also mentioned that that would be better handled without using a regular expression or using a regular expression would be overkill. So how do you decide when to use a regular expression and when not to use a regular expression. Is there a check list you can follow?

推荐答案

正则表达式是一种文本处理工具,用于基于字符的测试.更正式地说,正则表达式擅长处理正则语言,但几乎不擅长处理其他任何事情.

Regular expressions are a text processing tool for character-based tests. More formally, regular expressions are good at handling regular languages and bad at almost anything else.

实际上,这意味着正则表达式不太适合需要在超出字符级别的文本中发现含义(语义)的任务.这将需要一个成熟的解析器.

In practice, this means that regular expressions are not well suited for tasks that require discovering meaning (semantics) in text that goes beyond the character level. This would require a full-blown parser.

在您的特定情况下:识别文本中的数字是一项正则表达式擅长的练习(使用正则语言可以简单地描述十进制数).这适用于角色级别.

In your particular case: recognizing a number in a text is an exercise that regular expressions are good at (decimal numbers can be trivially described using a regular language). This works on the character level.

但是对需要了解其数值(即其语义)的数字进行更高级的操作需要解释.正则表达式在这方面很糟糕.因此,在文本中查找数字很容易.在文本中查找大于 11 但小于 1004(或可被 3 整除)的数字很困难:它需要识别该数字的含义.

But doing more advanced stuff with the number that requires knowledge of its numerical value (i.e. its semantics) requires interpretation. Regular expressions are bad at this. So finding a number in text is easy. Finding a number in text that is greater than 11 but smaller than 1004 (or that is divisible by 3) is hard: it requires recognizing the meaning of the number.

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

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