Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

how to do match from $li?

by virudinesh (Acolyte)
on Jun 11, 2013 at 07:23 UTC ( #1038208=perlquestion: print w/replies, xml ) Need Help??
virudinesh has asked for the wisdom of the Perl Monks concerning the following question:

am tried different type of matching concept but don't match

$li ="aaaa g f mj jgbmhj gkhkh " ; if($li=~m/http://www.[a-z]|[A-Z].[a-z]/i) { print "$& \n"; }

output comes in error near this //

i want to fetch only from $li line

Replies are listed 'Best First'.
Re: how to do match from $li?
by davido (Archbishop) on Jun 11, 2013 at 07:33 UTC

    Have you read perlretut yet? You've got to get through some of that stuff if you want to move forward.

    . is a special metacharacter inside of Perl regular expressions. It means to match anything except for newline.

    Character classes match only a single character unless you add a quantifier.

    Alternation is constrained to the entire regular expression, or the first enclosing ( ... ) or (?: ... ) construct.

    Case insensitivity applies to character classes too.

    Combine those issues, and what you have is:

    m/ www # match literal 'www' . # match any single character except \n. [a-z] # match any single character between a and z. | # OR [A-Z] # match any single character between A and Z. . # match any single character except \n. [a-z] # match any single character between a and z. /ix # /i makes everything case-insensitive, so there's # no difference between [A-Z] and [a-z].

    If you want to accomplish this without learning regular expressions, install the URI::Find distribution, and use its URI::Find::Schemeless module.


      Further to davido's point about alternation:
      Because it's a point that often escapes people (it's escaped me often enough), I want to emphasize that the effective low precedence of the  | (alternation) regex operator means that the  [A-Z].[a-z] portion of the OPed regex matches independently of the rest of that particular regex. E.g., after fixing the  // delimiter confusion, but leaving the  . (dot) matching as it was:

      >perl -wMstrict -le "my $li = 'foo'; ;; print qq{matched '$&' in '$li'} if $li =~ m{http://www.[a-z]|[A-Z].[a-z]}i; " matched 'foo' in 'foo'

      NB: Don't get into the habit of using the  $& $` $' special matching variables in your regexes. See the paragraph in perlre that begins "WARNING: Once Perl sees that you need one of $&, $`, or $' anywhere in the program ..." for a discussion of the cost of using them, and also the following paragraph for a workaround available in Perl version 5.10+. Also see the discussion of these variables in perlretut for workarounds using substr that can be used pre-5.10.

        Lol. I totally missed the /http//:...../ delimiter bug. It starts looking like an intentional attempt to get it wrong just so we will jump around like our hair is on fire trying to fix it. ...because there really is nothing that is right within it. An honest attempt to get it right would contain at least one portion of the RE that isn't a bug. ;)


Re: how to do match from $li?
by cdarke (Prior) on Jun 11, 2013 at 10:18 UTC
    In addition to the replies above, your actual error is because of the embedded / in:
    There are several solutions, the simplest is to use some other character as your delimiter, for example:
Re: how to do match from $li?
by hdb (Monsignor) on Jun 11, 2013 at 08:14 UTC

    • In order to match a literal dot, use \. or [.]
    • As you have the /i applied to your regex, [a-z] and [A-Z] are really the same, so you only need one of them.
    • In order to match one or more characters, use [a-z]+
    • In order to match exactly three characters like org, use braces like [a-z]{3}

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1038208]
Approved by Corion
[usemodperl]: sorry veltro, venting...
[usemodperl]: (is that wrong marto?)
[aitap]: usemodperl: maybe it's you who has changed
[marto]: demonstrably yes, since you claim to want a safe space, your definition for which seems to be a place where you can name call, make things up or otherwise post without being challenged
[usemodperl]: no that's not it at all, meant safe for perl people, to not be hounded by weirdos obsessed with things like... whitespace :-)
[choroba]: I'm definitely not obsessed with whitespace, I rate myself among Perl people, and I haven't noticed any hounding
[marto]: "no it's not that"...."weirdos "...
[marto]: List EXE_FILES installed by CPAN so a couple of people suggest that your code looks obfuscated. I'd have to ageree, from the perspective of those who can't follow all of that one liner, it doesn't read well
Veltro is a weirdo, obsessed with whitespace
[marto]: to use the word "obsession" when so few people have said so little about it is grasping at staws

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2018-06-24 15:58 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.