How to combine regexps?

Discussion in 'Python' started by kj, Aug 5, 2009.

  1. kj

    kj Guest

    One of the nice things one can do with Perl's regexp's is illustrated
    in the following example:

    my $gly = qr/gg[ucag]/i
    my $ala = qr/gc[ucag]/i;
    my $val = qr/gu[ucag]/i;
    my $leu = qr/uu[ag]|cu[ucag]/i;
    my $ile = qr/au[uca]/i;

    my $aliphatic = qr/$gly|$ala|$val|$leu|$ile/;


    In other words, one can build regular expressions by re-combining
    other regular expressions.

    Is there a way to do this with Python's regexps?

    TIA!

    kynn
    kj, Aug 5, 2009
    #1
    1. Advertising

  2. kj

    Carl Banks Guest

    On Aug 5, 1:09 pm, kj <> wrote:
    > One of the nice things one can do with Perl's regexp's is illustrated
    > in the following example:
    >
    > my $gly = qr/gg[ucag]/i
    > my $ala = qr/gc[ucag]/i;
    > my $val = qr/gu[ucag]/i;
    > my $leu = qr/uu[ag]|cu[ucag]/i;
    > my $ile = qr/au[uca]/i;
    >
    > my $aliphatic = qr/$gly|$ala|$val|$leu|$ile/;
    >
    > In other words, one can build regular expressions by re-combining
    > other regular expressions.
    >
    > Is there a way to do this with Python's regexps?


    Your example is just string manipulation.

    You're not combining regexps here, you are combining strings which you
    (evidently) will soon use to create a regexp.

    Yes, you can do that in Python as well, in exactly the same way, using
    Python's string manipulation capabilities.


    Carl Banks
    Carl Banks, Aug 5, 2009
    #2
    1. Advertising

  3. kj

    kj Guest

    In <h5cotl$or0$> kj <> writes:



    >One of the nice things one can do with Perl's regexp's is illustrated
    >in the following example:


    >my $gly = qr/gg[ucag]/i
    >my $ala = qr/gc[ucag]/i;
    >my $val = qr/gu[ucag]/i;
    >my $leu = qr/uu[ag]|cu[ucag]/i;
    >my $ile = qr/au[uca]/i;


    >my $aliphatic = qr/$gly|$ala|$val|$leu|$ile/;



    >In other words, one can build regular expressions by re-combining
    >other regular expressions.


    >Is there a way to do this with Python's regexps?



    OK, answering my own question here, it looks like the only way to
    do this is to leave the "component" expressions as regular strings,
    and combine those, before compiling them into regexps.

    Easy enough.

    kynn
    kj, Aug 5, 2009
    #3
  4. kj

    MRAB Guest

    kj wrote:
    > In <h5cotl$or0$> kj <> writes:
    >
    >
    >
    >> One of the nice things one can do with Perl's regexp's is illustrated
    >> in the following example:

    >
    >> my $gly = qr/gg[ucag]/i
    >> my $ala = qr/gc[ucag]/i;
    >> my $val = qr/gu[ucag]/i;
    >> my $leu = qr/uu[ag]|cu[ucag]/i;
    >> my $ile = qr/au[uca]/i;

    >
    >> my $aliphatic = qr/$gly|$ala|$val|$leu|$ile/;

    >
    >
    >> In other words, one can build regular expressions by re-combining
    >> other regular expressions.

    >
    >> Is there a way to do this with Python's regexps?

    >
    >
    > OK, answering my own question here, it looks like the only way to
    > do this is to leave the "component" expressions as regular strings,
    > and combine those, before compiling them into regexps.
    >
    > Easy enough.
    >

    A compiled regular expression has 'pattern' and 'flags' attributes, so
    you can get the original parameters which were used to create it if a
    function is given just the compiled regular expression. Might be useful.
    MRAB, Aug 5, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fredrik Ramsberg

    Optimisation of regexps in Perl?

    Fredrik Ramsberg, Oct 14, 2003, in forum: Perl
    Replies:
    2
    Views:
    461
    Fredrik Ramsberg
    Oct 15, 2003
  2. Replies:
    4
    Views:
    551
  3. Spartanicus

    Help with Regexps wanted

    Spartanicus, Oct 21, 2004, in forum: HTML
    Replies:
    4
    Views:
    442
    William Park
    Oct 25, 2004
  4. Klaus Alexander Seistrup

    Expanding regexps

    Klaus Alexander Seistrup, Oct 28, 2003, in forum: Python
    Replies:
    5
    Views:
    377
    Klaus Alexander Seistrup
    Oct 29, 2003
  5. Kirk Strauser

    Problems with regexps

    Kirk Strauser, Nov 7, 2003, in forum: Python
    Replies:
    6
    Views:
    301
    Edward K. Ream
    Nov 8, 2003
Loading...

Share This Page