1

So unfortunately Javascript doesn't support the \K token in regex, I'm searching for a way around this.

My Problem:

I have this regexp: Total statistics:\s+\K\d+ which should give me the "driven meters" of my lawn mower from a log string like this:

{"logSystem":[[2019,4,26,10,37,0,44872,"Battery Charge Started! 17.0°C 
21.05Volt","#000099","normal"],[2019,4,26,10,37,0,44871,"Total statistics: 
334418m, 23862min, blade on time: 21289min","#2E2EFE","bold"], 
[2019,4,26,10,37,0,44870,"Current cut statistics: 2m, 0min, blade on time: 
0min","#2E2EFE","bold"],

Working example (but not in JS) here: https://regex101.com/r/oL9gN5/11

Additional hints:
The meters are not always 6 chars long, may be 7 in a few years
Sometimes the string Total statistics: occurs twice in the log, the first match(newer data) is needed.

For clarification:
in most cases I would have used JSON parsing(and you should use too), but the rest of the log is mostly useless data and I need only this one data point. The mower creates a log entry every time it hits an obstacle or turns at the edge of the zone(I cut that part out). Also it shows only the last 100 lines of log with no option to go forward/back.

It took me a while to figure this regexp out, only to find it doesn't work in JS(iobroker), thx for any help!

RunsnbunsN
  • 88
  • 1
  • 11
  • 2
    Use a capturing group, `Total statistics:\s+(\d+)` – Wiktor Stribiżew Apr 26 '19 at 20:13
  • I hope you're not just using a regex on that raw string. It's valid JSON, so you can put it in an object with `JSON.parse()` and then access the specific string you want via `stats.logSystem[1][7]`, assuming the structure is always the same. – Herohtar Apr 26 '19 at 20:20
  • In an engine that supports the `\K` construct, it just changes the offset into the string of group0 among an array of offsets representing **_groups_**. Did you think the actual group data was copied somewhere ? Using groups doesn't copy data, it just sets the offsets into the string where the group starts and it's length. If later you want to get a copy from the matcher (where the array is maintained) of a group, it just takes the pointer of the string start, then adds the offset, then dereferences it and uses it and length to get it from a C api primitive . –  Apr 26 '19 at 20:35
  • 3
    There is no `JavaScript RegExp Lookbehind Alternative?` Even if there was, that's not what _that question was about_. The selected answer matched all the items and wrote back the offenders and changed the non-offenders. Classic case of _it has to be matched to move past it_. –  Apr 26 '19 at 20:45
  • 1
    @Herohtar in that specific case I do, because I really only need that one number, if I would need anything else JSON parsing would be better as you stated. – RunsnbunsN Apr 26 '19 at 23:12

1 Answers1

1

You can use this regex and capture your data from group1,

Total statistics:\s+(\d+)

Here, it will match the first occurrence of Total statistics: followed by one or more spaces and then (\d+) will capture the numbers in first grouping pattern.

Regex Demo

Noam
  • 1,317
  • 5
  • 16
Pushpesh Kumar Rajwanshi
  • 18,127
  • 2
  • 19
  • 36
  • It [has been already nicely explained](https://stackoverflow.com/a/24482330/3832970). – Wiktor Stribiżew Apr 26 '19 at 20:17
  • There is no reason to use a regex on that entire string; just parse the JSON and access the statistics string; then the regex to extract the desired data becomes much simpler. – Herohtar Apr 26 '19 at 20:23
  • @Herohtar: Yes, that's correct and as you said `stats.logSystem[1][7]` will again give him `Total statistics: 334418m` from which if he wants to extract the digits, again he will have to use some kind of splitting or replacement, so instead regex can do the job easily in such cases. Proper json parsing is really useful and worth, if you are accessing most of the information contained in the json and not just one. On the other side, if you need multiple information from a json and you use regex, then that's surely not a good idea. – Pushpesh Kumar Rajwanshi Apr 26 '19 at 20:28
  • @sln: You're right indeed. `^.*?` isn't needed. Let me remove that. Thanks for pointing out. – Pushpesh Kumar Rajwanshi Apr 26 '19 at 20:31
  • 2
    For clarification, in most cases I would have used JSON parsing, but the rest of the log is mostly useless data and I need only this one data point. The mower creates a log entry every time it hits an obstacle or turns at the edge of the zone(I cut that part out). Also it shows only the last 100 lines of log with no option to go forward/back. – RunsnbunsN Apr 27 '19 at 00:52