in reply to Newbie reg.exp question!

Well, if it's always the first 11 characters you want, there's no need to use a regexp. Use substr instead:
use strict; my $string="AB123456789this_code_is_untested"; my $match=substr $string, 0, 11;
If you really want to use a regexp (which is less efficient), use something like:
$string=~/^([a-zA-Z]{2}\d{9}).*/; print $1;

CU
Robartes-

Replies are listed 'Best First'.
Re: Re: Newbie reg.exp question!
by RuneK (Sexton) on Nov 13, 2002 at 13:54 UTC
    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
      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-

      almost the same as above:

      my($chars_nums) = $string =~ m{ ( [a-z]{2}\d{9} ) # capture 2 chars + 9 digits }ix;
      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