Don Coyote has asked for the wisdom of the Perl Monks concerning the following question:
Using Padre's Regex Editor today, very nice tool. I thought I might be able to split a pattern on a string of the same character, but that didn't seem to want to play. I could use something like unpack, but wondered if there were any tricks to doing this using split?
my @splitted = split /(?:(?=a{2})(?<=a{2}))/, 'aaaaaaaaa'; print join " ", @splitted; output recieved :aa a a a a a aa output desired :aa aa aa aa a
The main issue is that at each position there is a match, so the string is split at every position. I tried a mix of assertions and literals but the best I got was to match two at the start and end with single entities in between
I have a solution with s/// that I am happy about. I'm getting a count and any remainders modulo the length of the test string remain in the string. If I need to I can write a while loop rather than a map, though this is probably already better for what I want.
my $prim_to_test = ${ num_to_nat(2) }[0]; #get_wildmarks(); 'aa' my $split_pattern = qr/(\Q$prim_to_test\E)/; print "split_pat: $split_pattern\n"; my @splitted = map "$_ x $1", ( $wildmarks =~ s/(?=$split_pattern)$split_pattern//g ); print "splitted: ", join " \N{U+68} ", @splitted, "r: $wildmarks"; output:splitted: 4 x aa h r: a
But can split do it?
SPoiler ALert: Yes.
Further, it would appear the ability to use arbitrary characters is also expanded
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Can split split against more than one character in a string of the same character?
by johngg (Canon) on Jul 15, 2019 at 22:12 UTC | |
by Don Coyote (Hermit) on Jul 15, 2019 at 22:35 UTC | |
|
Re: Can split split against more than one character in a string of the same character?
by holli (Abbot) on Jul 15, 2019 at 20:52 UTC | |
by Don Coyote (Hermit) on Jul 15, 2019 at 22:23 UTC | |
|
Re: Can split split against more than one character in a string of the same character?
by hippo (Archbishop) on Jul 15, 2019 at 21:17 UTC | |
by Don Coyote (Hermit) on Jul 15, 2019 at 22:29 UTC |