简体   繁体   中英

Regex get last occurrence of the pattern

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

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#

Use the RightToLeft option:

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:

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

Do a greedy match up to the last set of [[ and capture..

.*(\[\[.*\]\])\];

See it here .

Try adding the $ to your pattern like this \\s\\[\\[(.*?)\\]\\]\\]\\;$ and let me know if it works.

Currently I don't have bash under hand so I cannot check but it should do the trick.

Edit : Right version \\S+\\s?+(?!((.*\\[\\[)))

which translates into :

\S   : all alfanumeric
\s?  : all 1 space occurences
?!   : not 
.*   : everything
\[\[ : until the last pattern of [[ (excluded)

Here is the Rubular example

BTW great tool rubular, make me wanna look more into ruby and regular expressions :D

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 MatchCollection containing all found matches. So you can do for example this:

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM