Returning substring of regex match

Discussion in 'Perl Misc' started by Taras_96, Jun 4, 2007.

  1. Taras_96

    Taras_96 Guest

    Hi everyone,

    Is it possible to ask for a substring, or substrings, of a regular
    expression match to be returned in a regular expression?

    EG:

    Say you want a list of the days in the dates for some input with some
    defined prefix:

    eg: Give me the days of all dates that are prefixed with the word DATE

    DATE22/5/07
    1/5/07
    DATE25/5/07
    DATENO 26/5/07

    Would return 22 and 25, but not 1 or 26.

    Thanks

    Taras
     
    Taras_96, Jun 4, 2007
    #1
    1. Advertising

  2. Taras_96 wrote:
    > Is it possible to ask for a substring, or substrings, of a regular
    > expression match to be returned in a regular expression?


    Yes. Use capturing parenteses.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jun 4, 2007
    #2
    1. Advertising

  3. Taras_96

    Paul Lalli Guest

    On Jun 4, 8:31 am, Taras_96 <> wrote:
    > Say you want a list of the days in the dates for some input
    > with some defined prefix:
    >
    > eg: Give me the days of all dates that are prefixed with the
    > word DATE
    >
    > DATE22/5/07
    > 1/5/07
    > DATE25/5/07
    > DATENO 26/5/07
    >
    > Would return 22 and 25, but not 1 or 26.


    Is this what you're looking for?

    #!/usr/bin/perl
    use strict;
    use warnings;
    while (<DATA>) {
    print "$1\n" if m!DATE(\d+)/\d+/\d+$!;
    }
    __DATA__
    DATE22/5/07
    1/5/07
    DATE25/5/07
    DATENO 26/5/07


    Take a look at
    perldoc perlretut
    perldoc perlre
    perldoc perlreref

    Paul Lalli
     
    Paul Lalli, Jun 4, 2007
    #3
  4. Taras_96

    Mirco Wahab Guest

    Taras_96 wrote:
    > Is it possible to ask for a substring, or substrings, of a regular
    > expression match to be returned in a regular expression?
    > Say you want a list of the days in the dates for some input with some
    > defined prefix:
    > eg: Give me the days of all dates that are prefixed with the word DATE
    >
    > DATE22/5/07
    > 1/5/07
    > DATE25/5/07
    > DATENO 26/5/07
    >
    > Would return 22 and 25, but not 1 or 26.


    You may "literally" put your question into a
    regular expression, like:

    ...
    my @input = qw'
    DATE22/5/07
    1/5/07
    DATE25/5/07
    DATENO 26/5/07
    ';

    print map /(?<=^DATE)\d+/g, @input;
    ...


    The regular expression asks
    for a numerical value (the day)
    when encountering the \d+ thing, *if*
    it was preceded by the letters DATE,
    the (?<= ... ) is called "positive
    lookbehind assertion".

    The /g modifier in list context (map)
    extracts the matched expression \d+,
    (which is the day) - without capturing
    parentheses.

    The result of the above expression would
    then be an "array of the extracted hits
    (days)", which is printed instantaneously
    here.

    Regards

    M.
     
    Mirco Wahab, Jun 4, 2007
    #4
  5. Mirco Wahab wrote:
    >
    > ...
    > my @input = qw'
    > DATE22/5/07
    > 1/5/07
    > DATE25/5/07
    > DATENO 26/5/07
    > ';
    >
    > print map /(?<=^DATE)\d+/g, @input;
    > ...


    The qw() operator puts 'DATENO' and '26/5/07' in two separate elements.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jun 4, 2007
    #5
  6. Taras_96

    Mirco Wahab Guest

    Gunnar Hjalmarsson wrote:
    > Mirco Wahab wrote:
    >>
    >> ...
    >> my @input = qw'
    >> DATE22/5/07
    >> 1/5/07
    >> DATE25/5/07
    >> DATENO‌░‌26/5/07 <-- !!
    >> ';
    >>
    >> print map /(?<=^DATE)\d+/g, @input;
    >> ...

    >
    > The qw() operator puts 'DATENO' and '26/5/07' in two separate elements.


    Oh yea, you are right.

    First, I thought of using _DATA_ but
    abstained from it because I didn't
    want to replicate Pauls code parts ...

    For the OP, the input data needs
    to be written like:

    ...
    my @input = (
    'DATE22/5/07',
    '1/5/07',
    'DATE25/5/07',
    'DATENO 26/5/07',
    );
    ...

    Sorry,

    M.
     
    Mirco Wahab, Jun 4, 2007
    #6
  7. Taras_96

    Dr.Ruud Guest

    Mirco Wahab schreef:

    > the input data needs
    > to be written like:
    >
    > ...
    > my @input = (
    > 'DATE22/5/07',
    > '1/5/07',
    > 'DATE25/5/07',
    > 'DATENO 26/5/07',
    > );


    Alternative:

    my @input = split /\n/, <<'-- ';
    DATE22/5/07
    1/5/07
    DATE25/5/07
    DATENO 26/5/07
    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Jun 4, 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. hiwa
    Replies:
    0
    Views:
    646
  2. Christine Mayer

    Getting substring by regex

    Christine Mayer, Sep 6, 2007, in forum: Java
    Replies:
    5
    Views:
    445
    Roedy Green
    Sep 7, 2007
  3. Replies:
    3
    Views:
    794
    Reedick, Andrew
    Jul 1, 2008
  4. Replies:
    3
    Views:
    211
    Sherm Pendley
    Aug 3, 2005
  5. Rob

    Need help with substring match

    Rob, Mar 9, 2007, in forum: Javascript
    Replies:
    2
    Views:
    90
Loading...

Share This Page