0

Could someone explain me, why i don't need the \G anchor in the below example ? Clearly i do not understand how regex is parsing the text. I have a big list of ips for example

 $ip4=1..30 | ForEach-Object { "192.168.1.$_" }

I join all the ips using a comma:

$ip4 -join ','
192.168.1.1,192.168.1.2,192.168.1.3,192.168.1.4,192.168.1.5,192.168.1.6,192.168.1.7,192.168.1.8,192.168.1.9,192.168.1.10,192.168.1.11,192.168.1.12,192.168.1.13,192.168.1.14,192.168.1.15,192.168.1.16,192.168.1.17,192.168.1.18,192.168.1.19,192.168.1.20,192.168.1.21,192.168.1.22,192.168.1.23,192.168.1.24,192.168.1.25,192.168.1.26,192.168.1.27,192.168.1.28,192.168.1.29,192.168.1.30

I want to split them so that i can have the most of ips for 255 char string.

$ip4 -join ',' -replace '(.{216,255}),','$1;' -split ';'  |%{'Next result';$_}

Next result
192.168.1.1,192.168.1.2,192.168.1.3,192.168.1.4,192.168.1.5,192.168.1.6,192.168.1.7,192.168.1.8,192.168.1.9,192.168.1.10,192.168.1.11,192.168.1.12,192.168.1.13,192.168.1.14,192.168.1.15,192.168.1.16,192.168.1.17,192.168.1.18,192.168.1.19,192.168.1.20
Next result
192.168.1.21,192.168.1.22,192.168.1.23,192.168.1.24,192.168.1.25,192.168.1.26,192.168.1.27,192.168.1.28,192.168.1.29,192.168.1.30

It works ok. But i don't understand why did that work, i thought i should use the \G anchor

like so

$ip4 -join ',' -replace '(\G.{216,255}),','$1;' -split ';'  |%{'Next result';$_}
Next result
192.168.1.1,192.168.1.2,192.168.1.3,192.168.1.4,192.168.1.5,192.168.1.6,192.168.1.7,192.168.1.8,192.168.1.9,192.168.1.10,192.168.1.11,192.168.1.12,192.168.1.13,192.168.1.14,192.168.1.15,192.168.1.16,192.168.1.17,192.168.1.18,192.168.1.19,192.168.1.20
Next result
192.168.1.21,192.168.1.22,192.168.1.23,192.168.1.24,192.168.1.25,192.168.1.26,192.168.1.27,192.168.1.28,192.168.1.29,192.168.1.30

If i am not using the \G regex should find all the possible matches for : Any character from at least 216 to 255 in length followed by ,
Counted from anywhere he wants unless he can match that pattern. That's why i should use the \G so he counts only the 216-255 character from the last match(the last comma). Can somebody explain where i am doing mistake in understanding regex ? The results are the same if i am using \G or without.

For exampleenter image description here

like on this picture, i can select multiple any characters between 2-8 length that end with comma. Why this is not working as per my marking on the picture ? I thought that this behavior is only when using \G (start counting those any character from last occurence).

For example i understand why there are so many matches of 2 characters here enter image description here I made it lazy, but if i make in my example(the top with ips) it lazy, it does not change anything

Gregu
  • 21
  • 2
  • Use `-split` and place `\G` in a lookbehind: `-split '(?<=\G.{240,255}(? – Mathias R. Jessen Jan 27 '20 at 10:20
  • I am trying to use this regex for ip6 and ip4, the lookbehind is not working correctly when approaching ip4 {216,255} , because it does not behave as greedy, hence i decided to perform a replace + split on ; , there the .{216,255} for for ip4 and for ip6 – Gregu Jan 27 '20 at 10:27
  • `\G` matches two types of positions: 1) start of the string, 2) end of the previous successful match. If you have two matches that are not immediately consecutive, it will fail. – Wiktor Stribiżew Jan 27 '20 at 11:05

0 Answers0