reg exp help

Discussion in 'Python' started by Jim, Jul 27, 2004.

  1. Jim

    Jim Guest

    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.
    Jim, Jul 27, 2004
    #1
    1. Advertising

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

    On 2004-07-27, Jim <> 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-----
    James Keasley, Jul 27, 2004
    #2
    1. Advertising

  3. Jim

    JRCondon Guest

    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
    JRCondon, Jul 27, 2004
    #3
  4. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    On 2004-07-27, James Keasley <> 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-----
    James Keasley, Jul 27, 2004
    #4
  5. 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
    Christopher T King, Jul 27, 2004
    #5
  6. 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! :)
    Christopher T King, Jul 27, 2004
    #6
    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. Andrew Rowland

    Reg exp: matching relative path only.

    Andrew Rowland, Aug 2, 2003, in forum: Perl
    Replies:
    0
    Views:
    1,141
    Andrew Rowland
    Aug 2, 2003
  2. PerlE

    Reg Exp Help

    PerlE, Jan 30, 2004, in forum: Perl
    Replies:
    0
    Views:
    498
    PerlE
    Jan 30, 2004
  3. Aristotle

    Help needed with reg exp please

    Aristotle, Sep 4, 2004, in forum: Perl
    Replies:
    4
    Views:
    483
    Gunnar Hjalmarsson
    Sep 4, 2004
  4. GrelEns

    help with cr in reg exp...

    GrelEns, Jan 17, 2004, in forum: Python
    Replies:
    1
    Views:
    296
    Peter Otten
    Jan 17, 2004
  5. aekalman
    Replies:
    6
    Views:
    128
    Ben Morrow
    Nov 22, 2004
Loading...

Share This Page