新秀对正则表达式的看法 - 有用但不完整 [英] Rookie thoughts on Regex--useful but not complete

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

问题描述

我经历了一系列正则表达式的例子,确实它非常强大,包括''群''使用''匹配'',字边界,

前瞻性匹配,替换和分割文本等 - 显然

有一本完整的书,虽然我刚用了这一章

Albahari et al C#3 in a Nutshell(一本好书)。你确实可以通过多种方式对一个字符串进行按摩,但是,在

过程的最后,我发现你仍然必须通过''按摩''

字符串,逐个字符,并做微调。我敢肯定你还可以做一个正则表达式来做这样的微调。但是我处于死路之中

因为一些问题而没有什么能比得上一个字符

分析。


For例如,假设您要从以下内容中提取所有编号的系数

10a,11d,12g和100f:给定字符串

" xyz10abc11def12gh100f"


string sMY =" xyz10abc11def12gh100f";


匹配mMY = Regex.Match(sMY,@" \\\\\\ );

Console.WriteLine(" result mMY - > {0}",mMY.Value);

Regex r001 = new Regex(@" \\\\\D");

MatchCollection mc2 = r001.Matches(sMY);


foreach(在mc2中匹配m)

{

Console.WriteLine(&m; {0}",m.Value);

}


你将获得输出(作为一个字符串数组):10a,11d,12g,00f,其中

不是你想要的,因为最后一个值是" 00F"不是

提取这些最终案例或者角落案例。


现在你的正则表达专家我肯定能找到确切的正则表达式来给出

正确的答案(这会有所帮助)如果有''有条件的''正则表达式

表达式,那么你可以做两位数或三位数,但我是

不知道一个),然而,在一般情况下,我认为我是正确的

说,虽然正则表达式帮助,它不能得到所有的角落情况。


RL

解决方案

11月17日上午8:44,raylopez99< raylope ... @ yahoo.comwrote:


你将获得输出(作为一个字符串数组):10a,11d,12g,00f,其中

不是你想要的,因为最后一个值是00f不是

提取这些最终案例或者角落案例。


现在你的正则表达专家我肯定能找到确切的正则表达式来给出

正确的答案(这会有所帮助)如果有''有条件的''正则表达式

表达式,那么你可以做两位数或三位数,但我是

不知道一个),但是,在一般情况下,我认为我说的是正确的

说,虽然正则表达式帮助,它不能得到所有的角落案件。



您可以使用以下表达式:


@" \d {2,3} \D"


返回10a,11d,12g和100f。


{2,3}适用于数字(\d)和告诉它至少要找
2位但不超过3位。您可以使用竖线字符

表示或正则表达式中的条件。例如,如果你的字符串是

" balltallcallfallhallmall"并且你使用了这个正则表达式:[t | h] all

将返回匹配tall和大厅


这是一个包含许多正则表达式的网站。您可以检查它们

并查看它们的工作原理。也许这对你的学习有所帮助。

http:// regexlib。 com / default.aspx


Chris




" raylopez99" < ra ******** @ yahoo.comwrote in message

news:5d ********************** ************ @ v5g2000p rm.googlegroups.com ...


现在你的Regex专家我肯定能找到确切的正则表达式给

正确答案



然而,在一般情况下,我认为我是正确的
< blockquote class =post_quotes>
说当Regex帮助时,它无法获得所有角落案例。



你的第一点与你的第二点相矛盾。


正则表达式语法难看且要求严格,毫无疑问那。考虑到另一种编程语法,因为丑陋和缺乏可读性,因此很难(如果不是不可能的话)考虑另一种编程语法。我的直觉告诉我,

很少有程序员认为自己很有能力,而且远远不足以说明他们已经掌握了它的主张。


尽管如此,它与你理解它一样强大。我建议您继续在网上搜索

的例子(或许考虑购买你提到的那本书!)<而不是
贬低它。 br />
并为自己开发解决问题的能力,例如你提出的简单问题(另一张海报已经给你解决方案)。


Tom Dacon

Dacon软件咨询


11月17日,11:36 * am,Tom Dacon ; < tda ... @ community.nospamwrote:


" raylopez99" < raylope ... @ yahoo.com写了留言


新闻:5d *********************** *********** @ v5g2000p rm.googlegroups.com ...


现在你的正则表达专家我肯定能找到确切的正则表达式给予

正确答案



*但是,在一般情况下,我认为我是正确的
< blockquote class =post_quotes>
说当Regex帮助时,它无法获得所有角落案例。



你的第一点与你的第二点相矛盾。


正则表达式语法难看且要求严格,毫无疑问那。考虑到另一种编程语法,因为丑陋和缺乏可读性,因此很难(如果不是不可能的话)考虑另一种编程语法。我的直觉告诉我,

很少有程序员认为自己很有能力,而且远远不足以说明他们已经掌握了它的主张。


尽管如此,它与你理解它一样强大。我建议您继续在网上搜索

的例子(或许考虑购买你提到的那本书!)<而不是
贬低它。 br />
并为自己开发解决问题的能力,例如你提出的简单问题(另一张海报已经给你解决方案)。


Tom Dacon

Dacon Software Consulting



你多大了?

最有可能是正则表达式从你出生之前就已经是aroudn :)

起初他们不容易理解,但后来你可以很容易地解构



I went through a bunch of Regex examples, and indeed it''s quite
powerful, including ''groups'' using ''matches'', word boundaries,
lookahead matches, replacing and splitting text,etc--apparently
there''s a whole book on it, though I just used the chapter in
Albahari et al C#3 in a Nutshell (a great book). You can indeed
''massage'' a string into a number of ways, but, at the end of the
process I found that you still have to go through the ''massaged''
string, character by character, and do "fine tuning". I''m sure you
can also do a Regex to do such "fine tuning" but I am at a dead end
for a number of problems and nothing beats a character by character
analysis.

For example, suppose you want to extract all the numbered coefficents
10a, 11d, 12g and 100f from the following: given the string
"xyz10abc11def12gh100f".

string sMY = "xyz10abc11def12gh100f";

Match mMY = Regex.Match(sMY, @"\d\d\D");
Console.WriteLine("result mMY-->{0}", mMY.Value);
Regex r001 = new Regex(@"\d\d\D");
MatchCollection mc2 = r001.Matches(sMY);

foreach (Match m in mc2)
{
Console.WriteLine("m: {0}", m.Value);
}

You''ll get output (as an array of strings): 10a, 11d, 12g, 00f, which
is not quite what you want, because of the last value is "00f" not
"100f". So you still have to go through the string "manually" and
extract these "end cases" or "corner cases".

Now you Regex experts I''m sure can find the exact Regex to give the
proper answer (it would help if there''s a ''conditional'' Regex
expression so you can do either two digits or three digits, but I''m
not aware of one), however, in the general case, I think I''m correct
in saying that while Regex helps, it cannot get all the corner cases.

RL

解决方案

On Nov 17, 8:44 am, raylopez99 <raylope...@yahoo.comwrote:

You''ll get output (as an array of strings): 10a, 11d, 12g, 00f, which
is not quite what you want, because of the last value is "00f" not
"100f". So you still have to go through the string "manually" and
extract these "end cases" or "corner cases".

Now you Regex experts I''m sure can find the exact Regex to give the
proper answer (it would help if there''s a ''conditional'' Regex
expression so you can do either two digits or three digits, but I''m
not aware of one), however, in the general case, I think I''m correct
in saying that while Regex helps, it cannot get all the corner cases.

You can use this expression:

@"\d{2,3}\D"

which returns 10a, 11d, 12g, and 100f.

The {2,3} applies to the digit (\d) and tells it to look for at least
2 digits but no more than three. You can use the pipe character to
indicate an "or" condition in a regex. For example if your string was
"balltallcallfallhallmall" and you used this regex: "[t|h]all" it
would return matches "tall" and "hall"

Here is a site with many regular expressions. You can examine them
and see how they work. Perhaps it will help in your study.

http://regexlib.com/default.aspx

Chris



"raylopez99" <ra********@yahoo.comwrote in message
news:5d**********************************@v5g2000p rm.googlegroups.com...

Now you Regex experts I''m sure can find the exact Regex to give the
proper answer

however, in the general case, I think I''m correct

in saying that while Regex helps, it cannot get all the corner cases.

Your first point contradicts your second.

The Regex syntax is ugly and demanding, there''s no question about that. It''s
difficult, if not impossible, to think of another programming syntax that
comes close for ugliness and lack of readability. My intuition tells me that
there are few programmers who consider themselves competent in it, and far
far fewer that can make the claim that they''ve mastered it.

Nevertheless, it''s as powerful as your understanding of it. Rather than
disparage it, I''d suggest that you continue your study by searching for
examples on the web (perhaps consider buying the book that you mentioned!)
and develop for yourself the ability to solve problems such as the simple
one you posed (another poster has already given you the solution).

Tom Dacon
Dacon Software Consulting


On Nov 17, 11:36*am, "Tom Dacon" <tda...@community.nospamwrote:

"raylopez99" <raylope...@yahoo.comwrote in message

news:5d**********************************@v5g2000p rm.googlegroups.com...

Now you Regex experts I''m sure can find the exact Regex to give the
proper answer


*however, in the general case, I think I''m correct

in saying that while Regex helps, it cannot get all the corner cases.


Your first point contradicts your second.

The Regex syntax is ugly and demanding, there''s no question about that. It''s
difficult, if not impossible, to think of another programming syntax that
comes close for ugliness and lack of readability. My intuition tells me that
there are few programmers who consider themselves competent in it, and far
far fewer that can make the claim that they''ve mastered it.

Nevertheless, it''s as powerful as your understanding of it. Rather than
disparage it, I''d suggest that you continue your study by searching for
examples on the web (perhaps consider buying the book that you mentioned!)
and develop for yourself the ability to solve problems such as the simple
one you posed (another poster has already given you the solution).

Tom Dacon
Dacon Software Consulting

How old are you?
Most probably the Regex have been aroudn since before you were born :)
They are not easy to understand at first but later you can deconstruct
most of then easily.


这篇关于新秀对正则表达式的看法 - 有用但不完整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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