Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > reg exp help

Reply
Thread Tools

reg exp help

 
 
Jim
Guest
Posts: n/a
 
      07-27-2004
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.
 
Reply With Quote
 
 
 
 
James Keasley
Guest
Posts: n/a
 
      07-27-2004
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 2004-07-27, Jim <(E-Mail Removed)> 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)

iD8DBQFBBrnGqfSmHkD6LvoRAiM2AKCMkHH38JfVcjYqSpsijo I1gwH0JACeILhr
rX1K7Sl9h56OT5FZKpDt38w=
=Yf9Z
-----END PGP SIGNATURE-----
 
Reply With Quote
 
 
 
 
JRCondon
Guest
Posts: n/a
 
      07-27-2004
No need for regex here; you can do this with strings:

>>> text = '5EHello'
>>> def lengthCheckedString(text):

from string import split
stext = split(text,'E',1)
if int(stext[0]) == len(stext[1]):
return stext[1]
else:
return None

>>> lengthCheckedString(text)

'Hello'

But, if you want to use a regex:
>>> def lengthCheckedStringRE(text):

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

>>> lengthCheckedStringRE(text)

'Hello'

JRC

 
Reply With Quote
 
James Keasley
Guest
Posts: n/a
 
      07-27-2004
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 2004-07-27, James Keasley <(E-Mail Removed)> wrote:

[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-----
 
Reply With Quote
 
Christopher T King
Guest
Posts: n/a
 
      07-27-2004
On 27 Jul 2004, James Keasley wrote:

> 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

 
Reply With Quote
 
Christopher T King
Guest
Posts: n/a
 
      07-27-2004
On 27 Jul 2004, James Keasley wrote:

> [snip OP and perl]
>
> Sorry, I subscribe to both, though it was posted to that other group


Thy strange tongue hath confuseth!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help! Complex Pattern Extraction with Key/Value Pairs and Reg Exp? aekalman Perl Misc 6 11-22-2004 10:59 PM
Help needed with reg exp please Aristotle Perl 4 09-04-2004 05:38 PM
Reg Exp Help PerlE Perl 0 01-30-2004 06:15 AM
help with cr in reg exp... GrelEns Python 1 01-17-2004 11:35 AM
Reg exp: matching relative path only. Andrew Rowland Perl 0 08-01-2003 11:14 PM



Advertisments