reg exp help

James Keasley me at privacy.net
Tue Jul 27 22:23:35 CEST 2004


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 2004-07-27, Jim <jim_8421 at hotmail.com> wrote:

> I am creating a message format as follows:
> start of string has the length of a message
> 'E' character indicates end of the length part
> the rest of the string is the true message, example:
>
> "5Ehello"
>
> I need a reg exp that will return for me the length, and also the msg
> portion (in this case 'hello' ) only if the length is correct (5 in
> this case).

This snippet should do it.

$string =~ /(\d+)E(.*)/;
    if (length($s) == $1){
	print "$1\n";
    }

There are a couple of other methods of doing it as well, such as:-

@array = split(/E/, $string);
    if (length($array[1]) == $array[0]){
	...
    }

This is susceptable to errors if you have more than one E in the string,
and different separator might be more advisable.

- -- 
James					jamesk[at]homeric[dot]co[dot]uk

Cat, n.: Lapwarmer with built-in buzzer.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFBBrnGqfSmHkD6LvoRAiM2AKCMkHH38JfVcjYqSpsijoI1gwH0JACeILhr
rX1K7Sl9h56OT5FZKpDt38w=
=Yf9Z
-----END PGP SIGNATURE-----



More information about the Python-list mailing list