searching for a pattern for multiple matches

Discussion in 'Perl Misc' started by jhu, Nov 24, 2007.

  1. jhu

    jhu Guest

    Can anyone tell me using Perl how to find all matching patterns. So if
    I have this one big string of say a html web page which contains
    multiple IP's and my pattern which is \d{1,3}\.\d{1,3}\.\d{1,3}\.
    \d{1,3} then how do I go about setting up my code in some sort of
    while loop to find all instances of matches ? Also can someone in
    simple terms explain what a back reference is ? Is it simply the
    pattern found ?

    Thanks in advance
    jhu, Nov 24, 2007
    #1
    1. Advertising

  2. jhu

    Ben Morrow Guest

    Quoth jhu <>:
    > Can anyone tell me using Perl how to find all matching patterns. So if
    > I have this one big string of say a html web page which contains
    > multiple IP's and my pattern which is \d{1,3}\.\d{1,3}\.\d{1,3}\.
    > \d{1,3} then how do I go about setting up my code in some sort of
    > while loop to find all instances of matches ?


    Use the /g modifier. See 'Regexp Quote-Like Operators' in perldoc
    perlop, particularly the section starting "In scalar context, each
    execution of "m//g...".

    > Also can someone in
    > simple terms explain what a back reference is ? Is it simply the
    > pattern found ?


    perldoc perlretut

    Ben
    Ben Morrow, Nov 24, 2007
    #2
    1. Advertising

  3. jhu

    jhu Guest

    On Nov 23, 6:35 pm, Ben Morrow <> wrote:
    > Quoth jhu <>:
    >
    > > Can anyone tell me using Perl how to find all matching patterns. So if
    > > I have this one big string of say a html web page which contains
    > > multiple IP's and my pattern which is \d{1,3}\.\d{1,3}\.\d{1,3}\.
    > > \d{1,3} then how do I go about setting up my code in some sort of
    > > while loop to find all instances of matches ?

    >
    > Use the /g modifier. See 'Regexp Quote-Like Operators' in perldoc
    > perlop, particularly the section starting "In scalar context, each
    > execution of "m//g...".
    >
    > > Also can someone in
    > > simple terms explain what a back reference is ? Is it simply the
    > > pattern found ?

    >
    > perldoc perlretut
    >
    > Ben


    Ben thanks but I'm not on Unix or Linux but rather on Windows and
    MV6.0.
    jhu, Nov 24, 2007
    #3
  4. jhu

    Ben Morrow Guest

    Quoth jhu <>:
    > On Nov 23, 6:35 pm, Ben Morrow <> wrote:
    > > Quoth jhu <>:
    > >
    > > > Can anyone tell me using Perl how to find all matching patterns. So if
    > > > I have this one big string of say a html web page which contains
    > > > multiple IP's and my pattern which is \d{1,3}\.\d{1,3}\.\d{1,3}\.
    > > > \d{1,3} then how do I go about setting up my code in some sort of
    > > > while loop to find all instances of matches ?

    > >
    > > Use the /g modifier. See 'Regexp Quote-Like Operators' in perldoc
    > > perlop, particularly the section starting "In scalar context, each
    > > execution of "m//g...".
    > >
    > > > Also can someone in
    > > > simple terms explain what a back reference is ? Is it simply the
    > > > pattern found ?

    > >
    > > perldoc perlretut

    >
    > Ben thanks but I'm not on Unix or Linux but rather on Windows and
    > MV6.0.


    Huh? What difference does that make? Do you mean you can't find the
    perldocs? perldoc works perfectly well on Win32, or ActivePerl installs
    a nice HTMLified version in c:\perl\html.

    If you mean 'I'm not actually using Perl, but some other system that
    provides Perlish regexen' then: sorry, go ask somewhere else. This
    newsgroup is for discussing Perl.

    Ben
    Ben Morrow, Nov 24, 2007
    #4
  5. jhu

    Dr.Ruud Guest

    jhu schreef:

    > Can anyone tell me using Perl how to find all matching patterns. So if
    > I have this one big string of say a html web page which contains
    > multiple IP's and my pattern which is \d{1,3}\.\d{1,3}\.\d{1,3}\.
    > \d{1,3} then how do I go about setting up my code in some sort of
    > while loop to find all instances of matches ?



    #!/usr/bin/perl
    use strict;
    use warnings;

    use Regexp::Common;

    local $\ = "\n";

    (my $big_string = join "", <DATA>) =~ s/\s*\n\s*/ /g;

    print $big_string;
    print "-" x 40;

    my @ips = $big_string =~ m/$RE{net}{IPv4}/g;

    print for @ips;

    __DATA__
    abc 123.45.6.78 xyz
    abc 234.45.68.91 xyz
    abc 123.456.78.9 xyz
    abc 1.2.3.4 xyz
    abc 123.45.678 xyz
    abc 234.45.68.910 xyz
    abc 1.2.3.4 xyz


    See also:
    http://search.cpan.org/~abigail/Regexp-Common/lib/Regexp/Common/net.pm


    > Also can someone in
    > simple terms explain what a back reference is ? Is it simply the
    > pattern found ?


    A backreference is a sub-pattern. (The word pattern is traditionally
    reserved for the whole search pattern.)

    With a backreference, you reuse something that is already found by an
    earlier capture in the pattern:

    m/ ([aeiou]) \1 /x; # two vowels, equal
    m/ [aeiou]{2} /x; # two vowels, maybe equal


    Now read both perlre and perlretut.

    In perlre you'll find:
    "Referring back to another part of the match is called a backreference."

    In perlretut you'll find:
    "Backreferences are simply matching variables that can be used inside a
    regexp. This is a really nice feature - what matches
    later in a regexp can depend on what matched earlier in the regexp."

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Nov 24, 2007
    #5
  6. jhu

    jhu Guest

    On Nov 23, 8:11 pm, Ben Morrow <> wrote:
    > Quoth jhu <>:
    >
    >
    >
    >
    >
    > > On Nov 23, 6:35 pm, Ben Morrow <> wrote:
    > > > Quoth jhu <>:

    >
    > > > > Can anyone tell me using Perl how to find all matching patterns. So if
    > > > > I have this one big string of say a html web page which contains
    > > > > multiple IP's and my pattern which is \d{1,3}\.\d{1,3}\.\d{1,3}\.
    > > > > \d{1,3} then how do I go about setting up my code in some sort of
    > > > > while loop to find all instances of matches ?

    >
    > > > Use the /g modifier. See 'Regexp Quote-Like Operators' in perldoc
    > > > perlop, particularly the section starting "In scalar context, each
    > > > execution of "m//g...".

    >
    > > > > Also can someone in
    > > > > simple terms explain what a back reference is ? Is it simply the
    > > > > pattern found ?

    >
    > > > perldoc perlretut

    >
    > > Ben thanks but I'm not on Unix or Linux but rather on Windows and
    > > MV6.0.

    >
    > Huh? What difference does that make? Do you mean you can't find the
    > perldocs? perldoc works perfectly well on Win32, or ActivePerl installs
    > a nice HTMLified version in c:\perl\html.
    >
    > If you mean 'I'm not actually using Perl, but some other system that
    > provides Perlish regexen' then: sorry, go ask somewhere else. This
    > newsgroup is for discussing Perl.
    >
    > Ben- Hide quoted text -
    >
    > - Show quoted text -


    Ok I did not understand that I can still view Perl docs online.
    jhu, Nov 24, 2007
    #6
  7. jhu

    Dave Weaver Guest

    On Sat, 24 Nov 2007 11:12:54 -0800 (PST), jhu <> wrote:

    > Ok I did not understand that I can still view Perl docs online.


    You don't even have to be online.
    Just bring up a command prompt window and use the "perldoc" command.
    e.g.:

    perldoc perl
    perldoc perldoc

    If you installed ActiveState's distribution, just go to your Start
    Menu -> ActivePerl -> Documentation to get the HTML version.
    Dave Weaver, Nov 26, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rocalp
    Replies:
    0
    Views:
    370
    rocalp
    Feb 18, 2004
  2. Peekachu
    Replies:
    1
    Views:
    322
    Oliver Wong
    Jul 10, 2006
  3. Rajarshi Guha

    searching for matches within a word list

    Rajarshi Guha, Jul 29, 2003, in forum: Python
    Replies:
    1
    Views:
    286
    Scott David Daniels
    Jul 30, 2003
  4. jackster the jackle

    Searching Text for Multiple Matches

    jackster the jackle, Nov 30, 2009, in forum: Ruby
    Replies:
    3
    Views:
    105
    Robert Klemme
    Dec 2, 2009
  5. Markus Fischer
    Replies:
    9
    Views:
    160
    7stud --
    Apr 8, 2011
Loading...

Share This Page