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


In reply to Can split split against more than one character in a string of the same character? by Don Coyote

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.