没有线的起点和终点终结Regex.Replace有一些非常奇怪的效果....这到底是怎么回事? [英] Regex.Replace without line start and end terminators has some very strange effects.... What is going on here?

查看:142
本文介绍了没有线的起点和终点终结Regex.Replace有一些非常奇怪的效果....这到底是怎么回事?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在回答这个问题的 C#正则表达式替换和* 上有人提出,为什么这个问题的存在。玩的时候我产生了以下代码:。A

While answering this question C# Regex Replace and * the point was raised as to why the problem exists. When playing I produced the following code:

    string s = Regex.Replace(".A.", "\w*", "B");
    Console.Write(s);

这有输出: B.BB.B

我得到了0长度的字符串是赛前和字符之后,但为什么是A 2货币所取代。

I get that the 0 length string is match before and after the . character, but why is A replaced by 2 Bs.

我能理解 B.BBB.B 如更换零长度字符串一左一右 A BBB
,而实际结果混淆了我 - 感谢任何帮助。

I could understand B.BBB.B as replacing zero-length strings either side of A or B.B.B But the actual result confuses me - any help appreciated.

或者像AakashM所言:

Or as AakashM has put it:

为什么 Regex.Matches(A,\ W *)。计数等于 2 ,而不是 1 3

Why is Regex.Matches("A", "\w*").Count equal to 2, not 1 or 3 ?

推荐答案

由于\w *是一个贪婪的正则表达式,并试图找到最大的序列。所以它匹配一无所有点之前,那么一无所有A 点则两者之间的 一无所有第二点,最后一无所有第二个点后。

because \w* is a greedy regex and it tries to find biggest sequence. So it matches "nothing" before dot, then "nothing"A between two dots then "nothing" before second dot and finally "nothing" after the second dot.

这篇关于没有线的起点和终点终结Regex.Replace有一些非常奇怪的效果....这到底是怎么回事?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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