title: String masking: Confoming text to a given mask
body: <p>Given the following text and mask string: -</p>
  
  <pre><code>text: the quick brown fox jumps over the lazy dog
  mask: xx xxx xxxx x xxx
  </code></pre>
  
  <p>I'm trying to find a terse way to arrive at the result: -</p>
  
  <pre><code>th qui brow f jum
  </code></pre>
  
  <p>The mask conforms the text to it's pattern. The resulting string should have the same amount of words as the mask.</p>
  
  <p>My current implentation is using <code>List::Zip</code> to zip the words of each list together and do a string substitution. ( I've copied the logic of the zip function into the below example so you don't need to install it to test)</p>
  
  <pre><code># Squashed version of List::Zip-&gt;zip function
  sub zip{map{[map{shift@{$_}}@_]}0..((sort map{0+@{$_}}@_)[0]-1)}
  
  my $mask = 'xx xxx xxxx x xxx';
  my $text = 'the quick brown fox jumps over the lazy dog';
  
  for my $mt ( zip( [split(' ', $mask)], [split(' ', $text)] ) ) {
      my ( $m, $t ) = @{ $mt };
      $mask =~ s/ $m / substr( $t, 0, length($m) ) /xe;
  }
  
  print $mask; # OUTPUT: th qui brow f jum
  </code></pre>
  
  <p>... but I can't help but think there's a shorter way. Maybe a funky regex trick?</p>
  
  <p>Suggestions welcome.</p>
  
  <p><strong>Update</strong></p>
  
  <p>The accepted answer <a href="https://stackoverflow.com/a/1871197/3124587">here</a> is intriguing for it's use of <code>pos</code>. Trying to figure out how to apply it to my problem. (Edit: Borodin pointed out why it's not applicable to this problem)</p>
  
  <p>I should also note that arbitrary whitespace is of no concern, ie given: </p>
  
  <pre><code>text: 'one    two three'
  mask: 'x xx    xxx'
  </code></pre>
  
  <p>I don't care if the result that comes back is <code>o tw thr</code>. The only requirement is the same number of words, and the same length words.</p>
  
  <p><strong>Review</strong> </p>
  
  <p>In the end I've accepted Alexandr's 'funky regex' solution. It's terse, and very fast, running the fastest Benchmarks by a comfortable margin.</p>
  
  <p>Borodin's first solution, while very similar, created a regex pattern that didn't perform as well.</p>
  
  <pre><code>Borodin: (\S{1,2}) \S*\s+ (\S{1,3}) \S*\s+ (\S{1,4}) \S*\s+ (\S{1,1}) \S*\s+ (\S{1,3})
  Alexandr: (\S{2})\S*\s+(\S{3})\S*\s+(\S{4})\S*\s+(\S{1})\S*\s+(\S{3})\S*
  </code></pre>
  
  <p>A few minor changes to Borodin's solution brings it on par with Alexandr's, but I gave it to Alexandr for arriving there first.</p>
  
  <p>All the solutions are full of great and interesting idea's, thanks everyone.</p>