Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a string to groups of 2 chars using split?

I have this simple string :

"a1a2a3"

Is there any regex expression which can be used with split command so it will split the string to a pairs ? :

["a1","a2","a3"] ?

I've tried this :

"a1a2a3".split(/(?=..)/)

But it returns ["a", "1", "a", "2", "a3"]

p.s.

I can do it with Match but im looking (if exists) for the regex expression which can help me using split.

like image 486
Royi Namir Avatar asked Aug 23 '13 16:08

Royi Namir


People also ask

How do you split a string into two letters in Python?

The Python standard library comes with a function for splitting strings: the split() function. This function can be used to split strings between characters. The split() function takes two parameters. The first is called the separator and it determines which character is used to split the string.

How do you split a string into a group in Python?

You can split a string using the newline character (\n) in Python. We will take a string which will be separated by the newline character and then split the string. The newline character will act as the separator in the Split function.


1 Answers

split for even length string:

str.split(/(?=(?:..)*$)/)

split for odd length string, the last entry has single character:

str.split(/(?=(?:..)*.$)/)

Those are basically look-aheads that check whether the number of characters ahead is odd or even. It takes advantage of the fact that the number of characters ahead at all the split positions have the same parity as the length of the string.

The pattern in the (even version) look-ahead is (?:..)*$, which checks for even number of characters (?:..)* before the end of the string $. (Note that non-capturing group (?:pattern) is used here, otherwise, capturing group will create extra entries in the split result). Similar explanation applies for the odd version.

Note that . excludes several new line characters: \n, \r, \u2028 or \u2029. It will produce unexpected result for string containing such characters. Replace . with [\s\S] (or other equivalent construct) to make it works for all cases.


For practical purpose, match is the right tool for the job:

str.match(/..?/g)

For example:

"1234567890".match(/..?/g)
> [ "12", "34", "56", "78", "90" ]

"1234567890".match(/..?/g)
> [ "12", "34", "56", "78", "9" ]

The solution can be extended to group of n characters:

str.match(/.{1,<n>}/g)

For example:

"123456789012345678901234567890".match(/.{1,7}/g)
> [ "1234567", "8901234", "5678901", "2345678", "90" ]

It simply takes advantage of the greedy quantifier and creates groups of n characters, before running out of characters to match for the last group.

Same as above, you may want to change . to [\s\S] to make it work for all cases.

like image 139
nhahtdh Avatar answered Oct 10 '22 17:10

nhahtdh