2

I was working out a practice problem from Google code jam problem . Whenever I had letters of the same key as ab as 11 1 I had to type in a space between them. So I converted each individual letter to the corresponding number sequence and put them in an array. I joined them with ':'. Whenever I had the same digits consecutively separated by ':' I then replace them with ' '. I used the following regex with global modifier.

    my $translated = '333:33:3:333'; # edce
    $translated =~ s/(\d+):\g{1}/$1 $1/g;
    print $translated;

Output

333 33:3 333

I had to do the regex again to get the output 333 33 3 333. I had the global modifier but it doesn't work. I tried a while loop

1 while($translated =~ s/(\d+):\g{1}/$1 $1/g);
print $translated

Output

333 33 3 333

Edit :

I get the solution for the problem my question is

Why doesn't the global modifier work?

1 Answer 1

5

The global modifier does work, but we need to be clear how exactly it functions.

When a regex is matched with /g, it attempts the next match starting from where the previous match ended.

v-- start
333:33:3:333
 ^^^^^        1st match
      |
      v-- start
333 33:3:333
       ^^^    2nd match
          |
          v--start
333 33:3 333
              3rd match fails

You can influence which substring is considered to match by using lookaround assertions. Especially, we can change the regex so that only the : and not the surrounding digits are matched:

(\d+)\K[:](?=\g{-1})

How does this work? The (?=...) is a zero-width lookahead. The pattern is matched as usual, but the position does not advance from that match. The \K operator keeps the current position, will “forget” the previously matched substring. It is similar to a (?<=...) lookbehind, except that lookbehinds can only deal with constant-length patterns, which is not the case here.

Then:

my $str = "333:33:3:333";
$str =~ s/(\d+)\K[:](?=\g{-1})/ /g;
say $str;
# prints "333 33 3 333"
4
  • Thanks @amon. I haven't used lookahead or lookbehind. Could you please post a tutorial link that I can follow..
    – xtreak
    May 4, 2014 at 9:23
  • 2
    @Wordzilla The perlretut (Perl Regex Tutorial) has a section on lookaheads and lookbehinds.
    – amon
    May 4, 2014 at 9:26
  • Is (\d+)\K[:](?=\g{-1}) supposed to match and replace colons also in e.g. 123:323:3:321 ? Wouldn't that be similar to just capturing the digit in a lookbehind: (?<=(\d)):(?=\1).
    – Jonny 5
    May 4, 2014 at 12:46
  • 1
    @Jonny5 yes, all colons in 123:323:3:321 or 123:123:3 would be substituted. I didn't want to change the functionality of the regex in respect to this behavior, so I didn't remove the + quantifier. Btw, \g{…} is preferable over \1 because \g allows relative references or named patterns.
    – amon
    May 4, 2014 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.