Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Perl Programming Language

Returning substring of regex match


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 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

On Jun 4, 8:31 am, Taras_96 <taras@gmail.com> 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

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 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 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 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."

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc