How to split and save in an array

^0806EA^0406F0^^^^^^EF07F7--^E3DC03--^EAFE02-- ^0406F0^^^FFE209^^^DFF107^^^F6F508^^^E4DE01^^^EF07F7--^E803E6-- 

in the code above, I want to break this ^, but the problem is that this hex data has the same pattern ^.

I want to get the result as follows:

 ^0806EA ^0406F0^^^^^ ^EF07F7-- ^E3DC03-- ^EAFE02-- ^0406F0^^ ^FFE209^^ ^DFF107^^ ^F6F508^^ ^E4DE01^^ ^EF07F7-- ^E803E6-- 

or:

 0806EA 0406F0^^^^^ EF07F7-- E3DC03-- EAFE02-- 0406F0^^ FFE209^^ DFF107^^ F6F508^^ E4DE01^^ EF07F7-- E803E6-- 
+4
source share
2 answers

Since you do not want the symbols to be used in the split, and you also think that they do not want them to be the group itself, you need what is called a zero-width statement. Since we are talking about pockets, word boundaries will not work. This is not at the beginning or end of the line, and therefore the only ZWA that will work is a look or a look.

Since you want to break your chunks before a specific pattern, I would suggest a lookahead consisting of: carriage + non-carriage OR end of line, for example:

 split /(?=\^(?:[^^]|$))/; 

Illustrated below:

 while ( <DATA> ) { my @list = split /(?=\^(?:[^^]|$))/; say "@list"; } __DATA__ ^0806EA^0406F0^^^^^^EF07F7--^E3DC03--^EAFE02-- ^0406F0^^^FFE209^^^DFF107^^^F6F508^^^E4DE01^^^EF07F7--^E803E6-- 

Outputs:

 ^0806EA ^0406F0^^^^^ ^EF07F7-- ^E3DC03-- ^EAFE02-- ^0406F0^^ ^FFE209^^ ^DFF107^^ ^F6F508^^ ^E4DE01^^ ^EF07F7-- ^E803E6-- 
+2
source

One of the methods:

 perl -pe 's/(?<=.)(?=\^\w)/ /g' infile 

Explanation:

 s/regex/replacement # Substitution command. (?<=.) # Positive look-behind for any char. (?=\^\w) # Positive look-ahead for '^' plus a word character. # Add a space between them. 

Result:

 ^0806EA ^0406F0^^^^^ ^EF07F7-- ^E3DC03-- ^EAFE02-- ^0406F0^^ ^FFE209^^ ^DFF107^^ ^F6F508^^ ^E4DE01^^ ^EF07F7-- ^E803E6-- 
+2
source

Source: https://habr.com/ru/post/1401789/


All Articles