意见想要“正则表达式” [英] Opinion wanted on "Regex"

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

问题描述

您好,


我正在寻求使用正则表达式搜索的意见。对于依赖于这个而不是滚动你自己的(字符串)模式搜索功能而言,现在是否依赖于这个相比

的最佳实践,是否有一般性的共识?如果性能

是一个问题,你当然可以自己编写专门的例程。

但是,对于偶尔的模式搜索,性能不是问题,
(与专业例程相比)。提前致谢。

Hi there,

I''m seeking opinions on the use of regular expression searching. Is there
general consensus on whether it''s now a best practice to rely on this rather
than rolling your own (string) pattern search functions. Where performance
is an issue you can alway write your own specialized routine of course.
However, for the occasional pattern search where performance isn''t an issue,
would most seasoned .NET developers rely on "Regex" and cousins. Are there
any disadvantages I should be aware of other than possible efficiency issues
(compared to a specialized routine). Thanks in advance.

推荐答案

Mark,


我认为如果您正在做的工作是简单(比如说,用一个字符替换另一个字符),然后我就不会用正则表达式来完成工作了。但是,对于更复杂的模式匹配(并且在处理邮政编码,电话号码时,并不需要过多的b $ b到达该状态,

ip地址等等),我肯定会使用正则表达式。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Mark庭" < no_spam@_nospam.com写信息

新闻:Oz ************** @ TK2MSFTNGP03.phx.gbl ...
Mark,

I think that if the work that you are doing is simple (say, replacing
one character with another), then I would not use a regular expression to do
the work. However, for more complex pattern matching (and it doesn''t take
too much to get to that state, say, in processing zip codes, phone numbers,
ip addresses, etc, etc), I would definitely use a regular expression.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mark Chambers" <no_spam@_nospam.comwrote in message
news:Oz**************@TK2MSFTNGP03.phx.gbl...

你好,


我正在寻求关于使用正则表达式搜索的意见。对于现在是依靠这个

而不是滚动你自己的(字符串)模式搜索功能的最佳做法,是否有一般的共识?

性能是一个问题,你总是可以编写自己的专业例程

当然。然而,对于偶尔的模式搜索,其中性能不是一个问题,大多数经验丰富的.NET开发人员都会依赖于正则表达式。和

堂兄弟。除了可能的效率问题(与专业例程相比)之外,我应该注意哪些缺点。谢谢

advance。
Hi there,

I''m seeking opinions on the use of regular expression searching. Is there
general consensus on whether it''s now a best practice to rely on this
rather than rolling your own (string) pattern search functions. Where
performance is an issue you can alway write your own specialized routine
of course. However, for the occasional pattern search where performance
isn''t an issue, would most seasoned .NET developers rely on "Regex" and
cousins. Are there any disadvantages I should be aware of other than
possible efficiency issues (compared to a specialized routine). Thanks in
advance.


我认为如果你正在做的工作很简单(比如说,替换
I think that if the work that you are doing is simple (say, replacing

一个字符与另一个字符),然后我不会使用正则表达式

来完成工作。但是,对于更复杂的模式匹配(并且它不会花费太多来达到那个状态,比如说,在处理邮政编码时,电话

数字,ip地址等等),我肯定会使用常规的

表达式。
one character with another), then I would not use a regular expression to
do the work. However, for more complex pattern matching (and it doesn''t
take too much to get to that state, say, in processing zip codes, phone
numbers, ip addresses, etc, etc), I would definitely use a regular
expression.



感谢您的反馈。是的,我正在严格谈论模式匹配。

你总是想走阻力最小的路径,所以如果

更简单的话,我就不会用它了一应俱全。然而,我前几天开始编写一个详细的例行程序,在大约20行后我决定使用一个

正则表达式会更简单。经过多年的C ++工作后,

Win32然而(没有正则表达式的奢侈品),我已经习惯了自己滚动它的b / b
它没有'我甚至想到尝试它。据推测,很少有b / b
,你自己的经验显然是基于你的
回应。我可能会从这里开始依赖它。再次感谢您输入


Thanks for the feedback. Yes, I''m talking strictly about pattern matching.
You always want to take the path of least resistance so I wouldn''t use it if
something simpler is readily available. However, I started writing an
elaborate routine the other day and after about 20 lines I decided that a
regular expression would be much simpler. After years of working in C++ on
Win32 however (without the luxury of "Regex"), I''m so used to rolling my own
that it didn''t even occur to me to try it. Presumably there are few
negatives and your own experience is obviously positve based on your
response. I''ll probably rely on it myself from here on. Thanks again for
your input.


* Mark Chambers写道,On 17-5-2007 14:31:
* Mark Chambers wrote, On 17-5-2007 14:31:

你好,


我正在寻求使用正则表达式搜索的意见。对于依赖于这个而不是滚动你自己的(字符串)模式搜索功能而言,现在是否依赖于这个相比

的最佳实践,是否有一般性的共识?如果性能

是一个问题,你当然可以自己编写专门的例程。

但是,对于偶尔的模式搜索,性能不是问题,
(与专业例程相比)。在此先感谢。

Hi there,

I''m seeking opinions on the use of regular expression searching. Is there
general consensus on whether it''s now a best practice to rely on this rather
than rolling your own (string) pattern search functions. Where performance
is an issue you can alway write your own specialized routine of course.
However, for the occasional pattern search where performance isn''t an issue,
would most seasoned .NET developers rely on "Regex" and cousins. Are there
any disadvantages I should be aware of other than possible efficiency issues
(compared to a specialized routine). Thanks in advance.



我通常更喜欢依赖正则表达式。它们被优化为几乎自动执行这些字符串搜索的
。只需要非常小心

之类的东西。*无处不在,这些都是为了表现而牺牲。


我喜欢正则表达式的主要原因是它们是统一的

编写字符串操作的方法,而你自己的字符串操作

函数可以全部不同,但工作正常。所以从维护

的角度来看,它应该更容易。


这需要你使用IgnoreWhiteSpace并逐字逐句

字符串,所以你很容易插入注释等等:

sstring regex = @"


[az](?#any character from alfabet)



[] [az] [0-9]

)+(?#其他一些漂亮的评论)


"


它使阅读更容易,只要确保你写的空格为

[]如例子中所示。


Jesse

I usually prefer to rely on Regular Expressions. They get optimized to
do these string searches almost automatically. Just be very careful with
stuff like .* everywhere, those are killing for performance.

The main reason I like regular expressions is that they are a unified
way to write string manipulation, whereas your own string manipulation
functions can all be different, yet work fine. So from a maintenance
perspective, it should be easier.

This does require you to work with IgnoreWhiteSpace on and verbatim
strings, so you van easily insert comments and such:

sstring regex = @"

[a-z] (?#any character from the alfabet)
(
[ ][a-z][0-9]
)+ (?# Some other nifty comment)

"

It makes it much easier to read, just make sure you write your spaces as
[ ] as in the example.

Jesse


这篇关于意见想要“正则表达式”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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