Home > Enterprise >  Regex to find the last occurence of a string in a multiline text
Regex to find the last occurence of a string in a multiline text

Time:07-12

I have URL where I get a multiline-string from that looks like this:

                    "Values" : 
                    {
                        "16800" : 0,
                        "17100" : 0,
                        "17400" : 0,
                        "17700" : 0,
                        "18000" : 0,
                        "18300" : 0,
                        "18600" : 0,
                        "18900" : 0,
                        "19200" : 0,
                        "19500" : 0,
                        "19800" : 0,
                        "20100" : 0.029999999999999999,
                        "20400" : 0.040000000000000001,
                        "20700" : 0.059999999999999998,
                        "21000" : 0.080000000000000002,
                        "21300" : 0.10000000000000001,
                        "21600" : 0.14000000000000001,
                        "21900" : 0.19,
                        "22200" : 0.22,
                        "22500" : 0.23999999999999999,
                        "22800" : 0.25,
                        "23100" : 0.25,
                        "23400" : 0.27000000000000002,
                        "23700" : 0.28000000000000003,
                        "24000" : 0.28999999999999998,
                        "24300" : 0.33000000000000002,
                        "24600" : 0.35000000000000003,
                        "24900" : 0.39000000000000001,
                        "25200" : 0.41999999999999998,
                        "25500" : 0.47999999999999998,
                        "25800" : 0.5,
                        "26100" : 0.52000000000000002,
                        "26400" : 0.54000000000000004,
                        "26700" : 0.55000000000000004,
                        "27000" : 0.57000000000000006,
                        "27300" : 0.57999999999999996,
                        "27600" : 0.59999999999999998,
                        "27900" : 0.62,
                        "28200" : 0.64000000000000001,
                        "28500" : 0.66000000000000003,
                        "28800" : 0.67000000000000004,
                        "29100" : 0.69000000000000006,
                        "29400" : 0.70999999999999996,
                        "29700" : 0.75,
                        "30000" : 0.79000000000000004,
                        "30300" : 0.81000000000000005,
                        "30600" : 0.83000000000000007,
                        "30900" : 0.85999999999999999,
                        "31200" : 0.91000000000000003,
                        "31500" : 1.01,
                        "31800" : 1.27,
                        "32100" : 1.3600000000000001,
                        "32400" : 1.3,
                        "32700" : 1.3,
                        "33000" : 1.76,
                        "33300" : 1.8100000000000001,
                        "33600" : 1.78,
                        "33900" : 1.97,
                        "34200" : 2.0499999999999998,
                        "34500" : 2.0800000000000001,
                        "34800" : 2.6699999999999999,
                        "35100" : 2.8399999999999999,
                        "35400" : 2.7600000000000002,
                        "35700" : 3.0800000000000001,
                        "36000" : 3.3999999999999999,
                        "36300" : 3.6600000000000001,
                        "36600" : 3.8500000000000001,
                        "36900" : 4.04,
                        "37200" : 4.25,
                        "37500" : 4.46,
                        "37800" : 4.7000000000000002,
                        "38100" : 5.0600000000000005,
                        "38400" : 5.6000000000000005,
                        "38700" : 6.2999999999999998,
                        "39000" : 6.3500000000000005,
                        "39300" : 6.9000000000000004,
                        "39600" : 7.5600000000000005,
                        "39900" : 6.2000000000000002,
                        "40200" : 3.0300000000000002,
                        "40500" : 4.0700000000000003,
                        "40800" : 3.3100000000000001,
                        "41100" : 7.1799999999999997,
                        "41400" : 8.2200000000000006,
                        "41700" : 8.1099999999999994,
                        "42000" : 8.2699999999999996,
                        "42300" : 8.6699999999999999,
                        "42600" : 8.9700000000000006,
                        "42900" : 8.8900000000000006,
                        "43200" : 8.9399999999999995,
                        "43500" : 8.7599999999999998,
                        "43800" : 9.2000000000000011,
                        "44100" : 9.1400000000000006,
                        "44400" : 8.8200000000000003,
                        "44700" : 9.4600000000000009,
                        "45000" : 9.5099999999999998,
                        "45300" : 9.4600000000000009,
                        "45600" : 7.8500000000000005,
                        "45900" : 2.6899999999999999,
                        "46200" : 9.6400000000000006,
                        "46500" : 9.8900000000000006,
                        "46800" : 10.01,
                        "47100" : 10.040000000000001,
                        "47400" : 10.210000000000001,
                        "47700" : 10.25,
                        "48000" : 10.550000000000001,
                        "48300" : 10.630000000000001,
                        "48600" : 10.67,
                        "48900" : 10.35,
                        "49200" : 6
                    }

I would like to always find the last Value, in this case the value "49200" : 6

How can I write a regex that matches the position before the value "6" that I would like to get, or behind "49200" : ?

I tried something like this:

/ (00" : )(?!(?s:.*)(00" : ))

Which works. But is there a more general, elegant way to achieve this?

CodePudding user response:

I have used below regex.

(.|\n) (\"\d \"\s :\s [\d.] )(.|\n)

(.|\n) -> will continue matching every thing and backtracked to first double quotes of the last pair of double quote.

(\"\d \"\s :\s [\d.] ) -> This will capture and match your required string in the format "123 : 123"

(.|\n) -> and will match remaining entire string.

Demo link

CodePudding user response:

Since you want to find the last number, it is the last number/value pair which is not followed by a ,.
You can express that condition like this:

\"(\d )\"[^,}] \}$

Capture group 1 has your last key number in your dataset.

  • \"(\d )\" simply captures the number
  • [^,}] find characters following, excluding , and }
    (Values in the middle always have the ,, that is how we distinguish here)
  • \}$ makes sure we are at the end, since } is right before the search string ends.

Here is a regex101 playground to tinker around with it:

https://regex101.com/r/Ogo2cZ/1

CodePudding user response:

You could just find the element adjacent to the } using "\d*"\s:\s[\d.]*.*\n.*}.

Regex101

You can make it a bit safer by specifying the key name "Values".

Regex101

  • Related