C# 正则表达式获取模式的最后一次出现
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13836145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regex get last occurrence of the pattern
提问by Vazgun
I have a string and I need to select the last occurrence of the pattern. The string is:
我有一个字符串,我需要选择最后一次出现的模式。字符串是:
[[[1302638400000.0, 0], [1302724800000.0, 610.64999999999998], [1302811200000.0, 2266.6500000000001], [1303156800000.0, 4916.9300000000003], [1303329600000.0, 6107.3199999999997], [1303934400000.0, 9114.6700000000001]], [[1302638400000.0, 20000.0], [1302724800000.0, 20000.0], [1302811200000.0, 20000.0], [1303156800000.0, 20000.0], [1303329600000.0, 20000.0], [1303934400000.0, 20000.0]], [[1302638400000.0, 20000.0], [1302724800000.0, 20610.650000000001], [1302811200000.0, 22266.650000000001], [1303156800000.0, 24916.93], [1303329600000.0, 26107.32], [1303934400000.0, 29114.669999999998], [1304452800000.0, 30078.23]], [[1302718580000.0, 0.0], [1302772440000.0, 3.0532500000000073], [1303107093000.0, 11.333250000000007], [1303107102000.0, 21.753250000000008], [1303352295000.0, 24.584650000000003], [1303352311000.0, 26.8766], [1303815010000.0, 30.536599999999996], [1303815028000.0, 27.703349999999993]]];
[[[1302638400000.0,0],[1302724800000.0,610.64999999999998],[1302811200000.0,2266.6500000000001],[1303156800000.0,4916.9300000000003],[1303329600000.0,6107.3199999999997],[1303934400000.0,9114.6700000000001]],[[1302638400000.0,20000.0],[1302724800000.0,20000.0 ],[1302811200000.0,20000.0],[1303156800000.0,20000.0],[1303329600000.0,20000.0],[1303934400000.0,20000.0]],[[1302638400000.0,20000.0],[1302724800000.0,20610.650000000001],[1302811200000.0,22266.650000000001],[1303156800000.0,24916.93 ], [1303329600000.0, 26107.32], [1303934400000.0, 29114.669999999998], [1304452800000.0, 30078.23]] [[1302718580000.0,0.0],[1302772440000.0,3.0532500000000073],[1303107093000.0,11.333250000000007],[1303107102000.0,21.753250000000008],[1303352295000.0,24.584650000000003],[1303352311000.0,26.8766],[1303815010000.0,30.536599999999996],[1303815028000.0,27.703349999999993]]] ;
The pattern that I use is:
我使用的模式是:
\s\[\[(.*?)\]\]
Which unfortunately selects only 1st occurrence. The highlighted text is the desired result. It doesn't matter how many square brackets at the end, just need the last array set.
不幸的是,它只选择了第一次出现。突出显示的文本是所需的结果。最后有多少方括号无关紧要,只需要最后一个数组集。
UPDATE: If it can help you, then the coding is in c#
更新:如果它可以帮助你,那么编码是在 c#
回答by Lucian Enache
Try adding the $ to your pattern like this \s\[\[(.*?)\]\]\]\;$and let me know if it works.
尝试像这样将 $ 添加到您的模式中\s\[\[(.*?)\]\]\]\;$,让我知道它是否有效。
Currently I don't have bash under hand so I cannot check but it should do the trick.
目前我手头没有 bash,所以我无法检查,但它应该可以解决问题。
Edit : Right version \S+\s?+(?!((.*\[\[)))
编辑:正确的版本 \S+\s?+(?!((.*\[\[)))
which translates into :
翻译成:
\S : all alfanumeric
\s? : all 1 space occurences
?! : not
.* : everything
\[\[ : until the last pattern of [[ (excluded)
BTW great tool rubular, make me wanna look more into ruby and regular expressions :D
顺便说一句,很棒的工具 rubular,让我想更多地了解 ruby 和正则表达式:D
回答by Chris Seymour
回答by stema
There should be a global flag or a method that returns all matches in your language. Use this and take the last match.
应该有一个全局标志或一个方法,以您的语言返回所有匹配项。使用它并进行最后一场比赛。
In C# Matches()returns a MatchCollectioncontaining all found matches. So you can do for example this:
在 C# 中Matches()返回一个MatchCollection包含所有找到的匹配项。所以你可以这样做:
string source = "[[[1302638400000.0, 0], [1302724800000.0, 610.64999999999998], [1302811200000.0, 2266.6500000000001], [1303156800000.0, 4916.9300000000003], [1303329600000.0, 6107.3199999999997], [1303934400000.0, 9114.6700000000001]], [[1302638400000.0, 20000.0], [1302724800000.0, 20000.0], [1302811200000.0, 20000.0], [1303156800000.0, 20000.0], [1303329600000.0, 20000.0], [1303934400000.0, 20000.0]], [[1302638400000.0, 20000.0], [1302724800000.0, 20610.650000000001], [1302811200000.0, 22266.650000000001], [1303156800000.0, 24916.93], [1303329600000.0, 26107.32], [1303934400000.0, 29114.669999999998], [1304452800000.0, 30078.23]], [[1302718580000.0, 0.0], [1302772440000.0, 3.0532500000000073], [1303107093000.0, 11.333250000000007], [1303107102000.0, 21.753250000000008], [1303352295000.0, 24.584650000000003], [1303352311000.0, 26.8766], [1303815010000.0, 30.536599999999996], [1303815028000.0, 27.703349999999993]]];";
Regex r = new Regex(@"\s\[\[(.*?)\]\]");
MatchCollection result = r.Matches(source);
if (result.Count > 0) {
Console.WriteLine(result[result.Count - 1]);
} else {
Console.WriteLine("No match found!");
}
Console.ReadLine();
回答by Alan Moore
Use the RightToLeftoption:
使用RightToLeft选项:
Regex.Match(s, @"\[\[(.*?)\]\]", RegexOptions.RightToLeft)
This option is exclusive to the .NET regex flavor, and does exactly what you asked for: searches from the end of the input instead of the beginning. Of particular note, the non-greedy ?modifier works just as you expect; if you leave it off you'll get the whole input, but with it you get:
此选项是 .NET regex 风格独有的,并且完全符合您的要求:从输入的末尾而不是开头进行搜索。需要特别注意的是,非贪婪?修饰符的作用与您预期的一样;如果您不使用它,您将获得整个输入,但有了它,您将获得:
[[1302718580000.0, 0.0], [1302772440000.0, 3.0532500000000073], [1303107093000.0, 11.333250000000007], [1303107102000.0, 21.753250000000008], [1303352295000.0, 24.584650000000003], [1303352311000.0, 26.8766], [1303815010000.0, 30.536599999999996], [1303815028000.0, 27.703349999999993]]]
[[1302718580000.0, 0.0], [1302772440000.0, 3.0532500000000073], [1303107093000.0, 11.333250000000007], [1303107102000.0, 21.753250000000008], [1303352295000.0, 24.584650000000003], [1303352311000.0, 26.8766], [1303815010000.0, 30.536599999999996], [1303815028000.0, 27.703349999999993]]]

![C# 如何从 List<String[]> 创建一个 csv 文件](/res/img/loading.gif)