reg exp help

J

Jim

Hi,

I'm trying to make a regular expression for the following:

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).

Any help is appreciated, thank you.
 
J

James Keasley

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

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-----
 
J

JRCondon

No need for regex here; you can do this with strings:
from string import split
stext = split(text,'E',1)
if int(stext[0]) == len(stext[1]):
return stext[1]
else:
return None
'Hello'

But, if you want to use a regex: import re
m = re.match('(?P<txtlen>\d+)E(?P<txt>.*$)',text)
txt = m.group('txt')
if int(m.group('txtlen')) == len(txt):
return txt
else:
return None
'Hello'

JRC
 
J

James Keasley

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


[snip OP and perl]

Sorry, I subscribe to both, though it was posted to that other group ;)

It was wrong anyway, the $s should have been $2.

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

All true wisdom is found on T-shirts and taglines.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFBBrw6qfSmHkD6LvoRAtVHAJ9zkcUNOV343Qw/ysvFV8T3Nbj1IwCfXSLc
qFt5u1h4c7aDtf5mghpncbQ=
=Ms/S
-----END PGP SIGNATURE-----
 
C

Christopher T King

This snippet should do it.

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

Uhh... what I think he means is:

import re

l,s = re.match(r'(\d+)E(.*)',string).groups()
if len(s) == int(l):
print s
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

perl parse (reg exp) 7
Help in hangman game 1
Matching multiple line reg exp 3
reg exp 5
Help 1
EXP(ORT) ciphers and M2Crypto/OpenSSL 0
Can't solve problems! please Help 0
reg exp 4

Members online

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top