正则表达式得到的格局最后一次出现 [英] Regex get last occurrence of the pattern

查看:139
本文介绍了正则表达式得到的格局最后一次出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,我需要选择的模式最后一次出现。 该字符串是:

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]]];

这是我使用的模式是:

\s\[\[(.*?)\]\]

不幸的是只选择1日发生。突出显示的文本是所希望的结果。不要紧,有多少方括号底,只需要最后一个阵列组。

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.

更新: 如果它可以帮助你,那么编码是用C#

UPDATE: If it can help you, then the coding is in c#

推荐答案

使用了从右至左选项:

Regex.Match(s, @"\[\[(.*?)\]\]", RegexOptions.RightToLeft)

这个选项是独家的.NET正则表达式的味道,并且做了你问什么了:从输入,而不是开始的结束搜索。 ?特别值得注意的是,非贪婪 修改就像你期望;如果你把它关掉,你会得到整个输入,但有了它,你得到:

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:

[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]]]

这篇关于正则表达式得到的格局最后一次出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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