Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: m//g behaves strange...

by Dominus (Parson)
on Nov 10, 2003 at 20:01 UTC ( #305948=note: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    my $s = "123 45 6 789";
    while ($s =~ m/\d+/g) {
      print "> $&\n";
    }
    
  2. or download this
    > 123
    > 45
    > 6
    > 789
    
  3. or download this
    my $s = "123  carrots   45 6  bananas 789";
    while (1) {
    ...
      $s =~ /\G([a-z]+)/gc and print "WORD $1\n" and next;
      $s =~ /\G$/gc and last;
    }
    
  4. or download this
    NUMBER 123
    SPACE
    ...
    WORD bananas
    SPACE
    NUMBER 789
    
  5. or download this
    NUMBER 123
    NUMBER 45
    NUMBER 6
    NUMBER 789
    
  6. or download this
    my $s = "123  carrots   45 6  bananas 789";
    while ($s =~ /(\d+)/g) {
      print "'$1' at position ", pos($s)-length($1), "\n";
    }
    
  7. or download this
    '123' at position 0
    '45' at position 15
    '6' at position 18
    '789' at position 29
    
  8. or download this
    my $s = "123  carrots   45 6  bananas 789";
    while ($s =~ /(\d+)/g) {
      print "'$1' at position ", pos($s)-length($1), "\n";
      pos($s) += 13;
    }
    
  9. or download this
    '123' at position 0
    '5' at position 16
    '89' at position 30
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://305948]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (1)
As of 2021-08-04 02:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My primary motivation for participating at PerlMonks is: (Choices in context)








    Results (41 votes). Check out past polls.

    Notices?