1 Replies - 1339 Views - Last Post: 23 January 2012 - 11:00 AM

#1 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Anomaly in progressive pattern matching

Posted 23 January 2012 - 09:39 AM

Hi, I was under the impression that using the \g modifier enables progressive matching, i.e with every successful match, the pointer is automatically set to the next character
after the match, so that the search for the next match continues from there. The following example demonstrates this:

$t = "I am 123we boy with 34q temper and 43e 21w and 56yt.";
$t =~ /(\d+)/g;  
print "$1\n";   #prints 123
$t =~ /(\w+)/g;
print "$1\n";  #prints we
$t =~ /(\d+)/g;
print "$1\n";  #prints 34
$t =~ /(\w+)/g;
print "$1\n";  #prints q
$t =~ /(\d+)/g;
print "$1\n";  #prints 43
$t =~ /(\w+)/g;
print "$1\n";  #prints e



But the following example, which looks exactly similar to the previous one, misbehaves:

$recipe = "Preheat oven to 451 deg. fahrenheit. Mix 1 ml. dilithium with 3 oz. NaCl and stir in 4 
anchovies. Glaze with 1 g. mercury. Heat for 4 hours and let cool for 3 seconds. Serves 10 aliens.";
$recipe =~ /(\d+)\s/g;
print "$1\n";  #prints 451, ok
$recipe =~ /(\w+)/; 
print "$1\n";  #prints preheat, instead of deg
$recipe =~ /(\d+)\s/g;
print "$1\n";  #prints 1,ok
$recipe =~ /(\w+)/; 
print "$1\n";  #prints preheat again, instead of ml
$recipe =~ /(\d+)\s/g;
print "$1\n"; #prints 3, ok
$recipe =~ /(\w+)\s/;
print "$1\n";  #prints preheat again, instead of oz



As if the pointer advances for the numeric matches, but resets to the beginning for the word matches, unlike the first example, which seems correct. Any help?

This post has been edited by cupidvogel: 23 January 2012 - 09:40 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Anomaly in progressive pattern matching

#2 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Anomaly in progressive pattern matching

Posted 23 January 2012 - 11:00 AM

Never mind, got it. Forgot to put the \g modifier after the word matching cases in the 2nd example!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1