Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Common Regex Gotchas

by chromatic (Archbishop)
on Apr 26, 2000 at 00:00 UTC ( #9096=perltutorial: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    my $data = "<tag>this is a line of code</tag>
    <explanation>this is where I wax poetic about my code</explanation>
    <tag>this is another example of code</tag>";
    if ($data =~ /<tag>(.*)<\/tag>/s) {
        print "I found =>$1<=\n";
    }
    
  2. or download this
    I found =>this is a line of code</tag>
    <explanation>this is where I wax poetic about my code</explanation>
    <tag>this is another example of code<=
    
  3. or download this
     if ($data =~ /<tag>(.*?)<\/tag>/s) {
        print "I found =>$1<=\n";
    }
    
  4. or download this
    my $line = "Name: Some Soldier, Rank:  Leftenant, Serial: 426879824, Boots:  black";
  5. or download this
    $line =~ /^\w*: \w*\s*\w*, \w*: \w*, \w+: (\d)*, \w*: \w*$/;
  6. or download this
    $line =~ /[Ss]erial: (\d{9})/;
  7. or download this
    $line =~ /<title>(.*?)</title>/;
  8. or download this
    $line =~ m!<title>(.*?)</title>!;
  9. or download this
    my $line = "a.b.cd*f.";
    $line =~ /([^.*]{2})/;
    
  10. or download this
    my $input = "foo bar baz";
    $input =~ s/(\w+)/uc($1)/ge;
    
  11. or download this
    $intput =~ s/([A-Za-z]+)/uc($1)/ge;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perltutorial [id://9096]
help
Chatterbox?
[Eily]: yes we're pretty prawn to making puns around here
Eily went to the wiktionary to check that the pronunciations of prawn and prone are actually similar. Turns out prawn can be an alternative form of porn...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2017-06-29 12:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (662 votes). Check out past polls.