Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Simple Regular Expression Q


    Hello to all community,

    I am newbie to Perl & Regular Expresiions too and I was looking
    for an answer to the following problem...

    I am trying to read a line of the form: xxyyzzz.pdf or xx.yy.zzz.pdf etc

    Originally I used $line=~ s/^.*\///; to capture the filename but
    this doesn't work in the second case, when dot(s) are inside the filename.

    How I can do that with a regular expression ?


    many thanks,

    John Ch.
  2. #2
  3. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    The regexp you've posted removes everything up to (and including) the first '/' character in the string. There aren't any '/' characters in the filenames you've posted.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Regexp newbie


    ok let me reformylate my question:

    Assuming that I have strings of the form xx.yy.zzz.pdf

    Which is the regexp to get the filenames including dots (i.e capture the "xx.yy.zzz") ?

    thanks ishnid for your immediate answer!
  6. #4
  7. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    You could do this:
    Code:
    my ( $no_extension ) = $filename =~ /^(.+)\./;
    Or if they always end in .pdf you could just strip that from the end directly.

    The main problem with capturing everything up to the last dot in your string is that you can get some filenames with double file extensions, such as "foo.tar.gz". Something to keep in mind.

    Comments on this post

    • Axweildr agrees
  8. #5
  9. wizard
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2009
    Location
    The Great White North
    Posts
    79
    Rep Power
    138
    Originally Posted by jch09
    ok let me reformylate my question:

    Assuming that I have strings of the form xx.yy.zzz.pdf

    Which is the regexp to get the filenames including dots (i.e capture the "xx.yy.zzz") ?

    thanks ishnid for your immediate answer!
    It would be better to use a module for it. Both File::Basename and File::Spec could and both are standard modules; they come with Perl.
    __END__

    I love Perl; it's the only language where you can bless your thingy.
  10. #6
  11. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    Don't you need to expressly supply a suffix (i.e. '.pdf' in this case) for File::Basename?
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,930
    Rep Power
    1225
    Originally Posted by ishnid
    Don't you need to expressly supply a suffix (i.e. '.pdf' in this case) for File::Basename?
    No, you can use a regex.

    Taken from the perldoc.
    Code:
    If @suffixes are given each element is a pattern (either a string or a qr//) matched against the end of the $filename. The matching portion is removed and becomes the $suffix.
    
         # On Unix returns ("baz", "/foo/bar/", ".txt")
         fileparse("/foo/bar/baz.txt", qr/\.[^.]*/);
  14. #8
  15. wizard
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2009
    Location
    The Great White North
    Posts
    79
    Rep Power
    138
    Originally Posted by FishMonger
    No, you can use a regex.

    Taken from the perldoc.
    Code:
    If @suffixes are given each element is a pattern (either a string or a qr//) matched against the end of the $filename. The matching portion is removed and becomes the $suffix.
    
         # On Unix returns ("baz", "/foo/bar/", ".txt")
         fileparse("/foo/bar/baz.txt", qr/\.[^.]*/);
    Wouldn't that be?
    perl Code:
    # On Unix returns ("baz", "/foo/bar/", ".txt")
    fileparse("/foo/bar/baz.txt", qr/\.[^.]*$/);
    __END__

    I love Perl; it's the only language where you can bless your thingy.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    How to convert string to an expression in PERL


    I have a quesion for my problem with using mysql and perl programming.

    - data from mysql is logic expression. After query I get it's value on string format:

    my $data = "$sex < 1 && $age >= 17 && $age <= 40 && ',1,2,3,' =~m/(,)$optid(,)/";

    In this expression $sex,$age,$optid is variables

    - How to make Perl understanding this function?
    my $sex,$age,$optid;
    if ($data) {
    ........
    ........
    }

    as

    if ($sex < 1 && $age >= 17 && $age <= 40 && ',1,2,3,' =~m/(,)$optid(,)/) {
    ........
    ........
    }

    help me please!
  18. #10
  19. 'fie' on me, allege-dly
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2003
    Location
    in da kitchen ...
    Posts
    12,889
    Rep Power
    6444
    you would need to parse that logic expression, and break on &&
    Code:
    (@atoms)=split(/&&/, $data);
    $if_string="if (";
    while (@atoms) {
      $if_string.="($_) && ";
    }
    #remove the last two & and a space
    $if_string=substr($if_string, 0, -3).") {";
    print $if_string;
    This what you're after, NOT TESTED BTW
    --Ax
    without exception, there is no rule ...
    Handmade Irish Jewellery
    Targeted Advertising Cookie Optout (TACO) extension for Firefox
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones


    09 F9 11 02
    9D 74 E3 5B
    D8 41 56 C5
    63 56 88 C0
    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
    -- Jamie Zawinski
    Detavil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ...
    BIT COINS ANYONE
  20. #11
  21. 'fie' on me, allege-dly
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2003
    Location
    in da kitchen ...
    Posts
    12,889
    Rep Power
    6444
    something seems to have gone a tad arseways here, these two threads just collided???
    --Ax
    without exception, there is no rule ...
    Handmade Irish Jewellery
    Targeted Advertising Cookie Optout (TACO) extension for Firefox
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones


    09 F9 11 02
    9D 74 E3 5B
    D8 41 56 C5
    63 56 88 C0
    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
    -- Jamie Zawinski
    Detavil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ...
    BIT COINS ANYONE
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    How to convert string to an expression in PERL


    Originally Posted by Axweildr
    you would need to parse that logic expression, and break on &&
    Code:
    (@atoms)=split(/&&/, $data);
    $if_string="if (";
    while (@atoms) {
      $if_string.="($_) && ";
    }
    #remove the last two & and a space
    $if_string=substr($if_string, 0, -3).") {";
    print $if_string;
    This what you're after, NOT TESTED BTW
    ------------------------------------------------------------
    Thank for your helped, but I want to make that perl understanding


    my $data = "$sex < 1 && $age >= 17 && $age <= 40 && ',1,2,3,' =~m/(,)$optid(,)/";

    my $sex,$age,$optid;
    if ($data) {
    ........
    ........
    }


    Likes

    if ($sex < 1 && $age >= 17 && $age <= 40 && ',1,2,3,' =~m/(,)$optid(,)/) {
    ........
    ........
    }


    Ex:

    Code:
    my $age=1;#wrong data for expression -> return fales
    my $sex = 0;
    my $optid = 100;#Wrong data for expression  -> return fales
    my $data = "$sex < 1 && $age >= 17 && $age <= 40 && ',1,2,3,' =~m/(,)$optid(,)/";
    if ($data) {
      print "True1";
    } else {
      print "False1";
    }
    
    if ($sex < 1 && $age >= 17 && $age <= 40 && ',1,2,3,' =~m/(,)$optid(,)/) {
      print "True 2";
    } else {
      print "false 2";
    }
    I wanted the same value printed as

    false 1
    false 2


    But the first expression - $data is a string and is defined so the result is "True 1"
    True 1
    false 2


    your final result is string too, not my target
  24. #13
  25. 'fie' on me, allege-dly
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2003
    Location
    in da kitchen ...
    Posts
    12,889
    Rep Power
    6444
    perl Code:
    #!/usr/bin/perl
    $data="test2";
    if ($data) {
    	print "[$data]OK\n";
    } else {
    	print "[$data]NOK\n";
    }
    $data="x";
    if ($data) {
    	print "[$data]OK\n";
    } else {
    	print "[$data]NOK\n";
    }
    $data="";
    if ($data) {
    	print "[$data]OK\n";
    } else {
    	print "[$data]NOK\n";
    }
    The code above shows what's happening to your if statements, you need to eval your if statement in an if block as per below
    perl Code:
    my $data = '$sex < 1 && $age >= 17 && $age <= 40 && \',1,2,3,\' =~m/(,)$optid(,)/';
    print $data;
    print "=====================================\n";
    (@atoms)=split(/&&/, $data);
    $if_string="if (";
    for (@atoms) {
      $if_string.="($_) && ";
    }
    #remove the last two & and a space
    $if_string=substr($if_string, 0, -3).") {\n";
    $if_string.=" print \"True2\";
     } else {
      print \"false 2\";
     }
     ";
    print $if_string;
    eval ($if_string);
    Hope that helps
    --Ax
    --Ax
    without exception, there is no rule ...
    Handmade Irish Jewellery
    Targeted Advertising Cookie Optout (TACO) extension for Firefox
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones


    09 F9 11 02
    9D 74 E3 5B
    D8 41 56 C5
    63 56 88 C0
    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
    -- Jamie Zawinski
    Detavil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ...
    BIT COINS ANYONE
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,930
    Rep Power
    1225
    perldoc -f eval

    This Q is cross posted to at least one other forum.
    perlguru.com
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    How to convert string to an expression in PERL


    Originally Posted by Axweildr
    perl Code:
    #!/usr/bin/perl
     ...... 
    print $if_string;
    eval ($if_string);
    Hope that helps
    --Ax
    It works - thank you very much!
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo