Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: About regular expression

by abstracts (Hermit)
on Mar 15, 2002 at 03:03 UTC ( [id://151897]=note: print w/replies, xml ) Need Help??


in reply to About regular expression

Hello

I'll try explaining what this code does. But first: about regular expressions.

Without getting too technical, Regular Expressions are one way of describing a set of words. So, when we say

if ($str =~ /some_re/){ do something ... }
what we're really doing is asking if the string belongs to the set described by the regular expression.

I don't (and can't) explain the whole of perl's regular expressions but I'll explain enough to make you understand the code segment.

  • /A/
    describes all words that contain the capital letter A.
  • /abc/
    describes all words that contain the sequence 'abc' ("abc", "sjdhabcsd", aaabc", ...).
  • /^abc/
    describes all words that begin with the sequence "abc" ("abc", "abcdefg", "abcccccc").
  • /abc$/
    describes all words that end with the sequence "abc" ("abc", "sjdhjabc", "aaabc").
  • /a*/
    describes all words that have zero or more of the letter 'a' ("", "abcj", or anything really).
  • /(ab)*/
    describes all words that have zero or more of the sequence 'ab' ("", "ab", "abab", "cdr", "tryabtry", ...).
  • /[abc]/
    contains any of 'a', 'b', or 'c'.
  • /[^abc]/
    none of these: for example
    /a[^bc]d/
    means 'a' followed by anything but 'b' or 'c', followed by a 'd'.
  • /./
    matches anything so
    /a.c/
    describes all words than contain 'a' followed by anything, followed by a 'c' ("aac", "abc", "a c", ...). To match a dot '.', you need to escape it with a \.
foreach (qw/one one.five two two.one two.ten two.ten.12 three three.nine four five/){ print "$_\n"; if($_ =~ /\./){ # if the word has a dot ".". # Keep everything after the last "." in the string my ($label) = /\.([^\.]*)$/; # a dot, followed by zero # or more letters that are # not dots, attached to the # end of the string. # return those letters # since they're between # brackets. $tree->add($_, -text => $label , # add the label to $tree -image => $mw->Getimage(folder)); } }
Hope this helped

Aziz,,,

Update: Thanks blakem for pointing out the need for escapes for \] in <pre> tags.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-20 16:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found