Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   How to combine regexps? (http://www.velocityreviews.com/forums/t693786-how-to-combine-regexps.html)

kj 08-05-2009 08:09 PM

How to combine regexps?
 


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

Carl Banks 08-05-2009 08:35 PM

Re: How to combine regexps?
 
On Aug 5, 1:09*pm, kj <no.em...@please.post> 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

kj 08-05-2009 08:36 PM

Re: How to combine regexps?
 
In <h5cotl$or0$1@reader1.panix.com> kj <no.email@please.post> 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

MRAB 08-05-2009 10:03 PM

Re: How to combine regexps?
 
kj wrote:
> In <h5cotl$or0$1@reader1.panix.com> kj <no.email@please.post> 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.


All times are GMT. The time now is 04:31 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.