in reply to Re: Extract pattern from string
in thread Extract pattern from string

Thanks for your response. That actually did not work. I modified the expression to this:

$str =~ /Protocol ((?:No\.|Number?) )?([A-Z0-9]{12})/;

The output I am now getting is "No.". Did I get your suggestion right? Thanks!

Replies are listed 'Best First'.
Re^3: Extract pattern from string
by hdb (Monsignor) on Jul 04, 2013 at 06:08 UTC

    I tried this:

    my $str="Study Protocol Number NBXF317N2201"; my ($term) = $str =~ /Protocol (?:(?:No\.|Number)? )?([A-Z0-9]{12})/; print $term;

    Another question: do you mean the . after No to be optional? Then it has to be

    my $str="Study Protocol Number NBXF317N2201"; my ($term) = $str =~ /Protocol (?:(?:No\.?|Number) )?([A-Z0-9]{12})/; print $term;

    UPDATE: Having thought about it, I would do the following which IMHO is simpler to read:

    use strict; use warnings; for my $str ( "Study Protocol Number NBXF317N2201", "Study Protocol No. NBXF317N2201", "Study Protocol NBXF317N2201", "Study Protocol No NBXF317N2201" ) { my ($term) = $str =~ /Protocol (?:No\. |No |Number |)([A-Z0-9] +{12})/; print "$term\n"; }

      @hdb: Thank you so much. This works. Really appreciate your help. Regards,madbee

Re^3: Extract pattern from string
by AnomalousMonk (Archbishop) on Jul 04, 2013 at 19:23 UTC
    That actually did not work. I modified the expression to this:
        $str =~ /Protocol ((?:No\.|Number?) )?([A-Z0-9]{12})/;

    BTW: The reason the modified regex didn't work is that you just moved the extraneous and confounding capturing parentheses from the inside to the outside of the group instead of making them non-capturing:  (?:(No\.|Number)? )? to  ((?:No\.|Number?) )?