in reply to Re: Newbie reg.exp question!
in thread Newbie reg.exp question!

Hi again,
The 2 chars and 9 digits code I got the last time has taken on a life of it's own. It now seems that there is not only one possible instance of the substring in a string, but possible more. The code I'm using to locate the sunstring is the following:

($number) = $line =~ m{ ( [A-Z]{2}\d{9} ) }ix;

The $line can now contain more substings. How do I ensure that the reg.xp. get's all the instances?. I cannot do a split because the data is entered by mortal users who tend to have no sense of order. So a string could look like:

"So I found SP236645744 in reference ED331234576, but not
in IJ558897567."

Hope you Gurus can help,
Rune

Replies are listed 'Best First'.
Re: Re: Re: Newbie reg.exp question!
by robartes (Priest) on Nov 13, 2002 at 14:01 UTC
    In that case, you would use a slightly modified version of the regexp:
    use strict; my $string="this_PM123456789_code_is_untested"; $string=~/([a-zA-Z]{2}\d{9})/; print $1;
    This will find the first occurence of your string.

    CU
    Robartes-

Re: Re: Re: Newbie reg.exp question!
by fglock (Vicar) on Nov 13, 2002 at 14:02 UTC

    almost the same as above:

    my($chars_nums) = $string =~ m{ ( [a-z]{2}\d{9} ) # capture 2 chars + 9 digits }ix;
Re: Re: Re: Newbie reg.exp question!
by RuneK (Sexton) on Nov 25, 2002 at 12:37 UTC
    Hi again, Thanks anyway, but I found a working solution my self. Like this:

    (@instances) = $line =~ m/[A-Z]{2}\d{11}/g; foreach $elem (@instances) { print $elem . ","; }

    It gives me every occurance of the substring in the string.
    BR,
    Rune
      Help again,
      My script just evolved. Now I need to support an extend string that looks like this:

      C[CCNNNNNNNNNNN] C= Char N= Number
      The former solution looked like this:

      @list = $line =~ m{ ( [A-Z]{2}\d{11} ) }gx;
      How can I support the special characters in the string? I tried like this, but it didn't return anything:

      @list = $line =~ m{ ( [A-Z]{1}\[[A-Z]{2}\]\d{11} ) }gx;
      Could someone please help and explain me how to do it correctly so that I don't have to ask next time?


      Thanks,
      Rune