http://qs321.pair.com?node_id=402358

C_T has asked for the wisdom of the Perl Monks concerning the following question:

There must be a way to do this, but I have not been able to locate anything.

I need to match (e.g.) B38-B49... that is B38, B39, B40, B41...B49.

I'm not sure how to tell it to do this, however, since 30-37 shoudn't match. I tried using the (?(condition)regex) syntax, but must be misunderstanding the doc because it's not working for me.

Anyone have a way to do this?

Thanks,

CT

Charles Thomas
Madison, WI

Replies are listed 'Best First'.
Re: Regex help
by ikegami (Patriarch) on Oct 25, 2004 at 22:07 UTC
    /B@{[join('|', 38..49)]}/
    or
    /B(?:38|39|40|41|42|43|44|45|46|47|48|49)/
    or
    /B(\d\d)/ && $1 >= 38 && $1 <= 49
      or
      /B(?:3[89]|4\d)/
      or
      /B(?!3[0-7])[34]\d/
      or
      /B(\d\d)(?(?{ $1 < 38 || $1 > 49 })(?=A)(?=Z))/
        /B(?:3[89]|4\d)/
        This worked like a charm, and really helped me understand the proper syntax of the conditional test.

        Thanks much!

        CT

        Charles Thomas
        Madison, WI
Re: Regex help
by JediWizard (Deacon) on Oct 25, 2004 at 22:30 UTC

    Another method:

    m/B(?:3[89]|4\d)/
    May the Force be with you
      That would match B4๖:
      m/B(?:3[89]|4\d)/ and print for "B4\x{E56}"
      With the arrival of Unicode, it's wrong to use \d if you mean [0-9].

        With the arrival of Unicode, it's wrong to use \d if you mean [0-9].

        This worries me.
        What happened when I wasn't paying attention?

        Will most of my code that's processing text containing digits break as soon as the input contains unicode?

        Cheers, Sören

Re: Regex help
by olivierp (Hermit) on Oct 26, 2004 at 11:06 UTC
    Yet another way, with the conditional
    /B[34](?(?<=3)[89]|[0-9])/
    HTH
    --
    Olivier