Regular Expression Problem...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • andrea.gavana@agip.it

    #1

    Regular Expression Problem...

    Hello NG,

    I am quite new with Python... I'm writing an application that does
    also some regexp things on strings, but I'm having problem about
    identifying/extracting a substring from another string. What I have to do
    is to extract all the strings that begins with a "$" character, but
    excluding characters like "." (point) and "'" (single quote) and "\" "/"
    (slashes). For example I have:

    1) This Is An $EXAMPLE String
    2) This Is An $EXAMPLE.String
    3) 'This Is An $EXAMPLE'
    4) This Is An \$EXAMPLE\Strin g;

    I would like to extract only the "keyword" $EXAMPLE and what I'm using at
    the moment is:

    #CODE BEGIN
    import re

    mystring = "This Is An \$EXAMPLE\Strin g;"
    regex = re.compile("[\$]+\S*",re.IGNORE CASE)
    keys = regex.findall(m ystring)

    #CODE END

    Obviously this code returns things like $EXAMPLE', $EXAMPLE/, $EXAMPLE. and
    so on...
    Does anyone have a suggestion?

    Thank you a lot.

    Andrea.

    ------------------------------------------------------------------------------------------------------------------------------------------

    Message for the recipient only, if received in error, please notify the
    sender and read http://www.eni.it/disclaimer/

  • Jorge Godoy

    #2
    Re: Regular Expression Problem...

    andrea.gavana@a gip.it writes:
    [color=blue]
    > #CODE BEGIN
    > import re
    >
    > mystring = "This Is An \$EXAMPLE\Strin g;"
    > regex = re.compile("[\$]+\S*",re.IGNORE CASE)
    > keys = regex.findall(m ystring)
    >
    > #CODE END[/color]

    regex = re.compile("[\$]+\w*",re.IGNORE CASE)
    [color=blue][color=green][color=darkred]
    >>> import re
    >>>
    >>> mystring = "This Is An \$EXAMPLE\Strin g;"
    >>> regex = re.compile("[\$]+\w*",re.IGNORE CASE)
    >>> keys = regex.findall(m ystring)
    >>> keys[/color][/color][/color]
    ['$EXAMPLE'][color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]


    Be seeing you,
    --
    Godoy. <godoy@ieee.org >

    Comment

    • Peter Otten

      #3
      Re: Regular Expression Problem...

      andrea.gavana@a gip.it wrote:
      [color=blue]
      > identifying/extracting a substring from another string. What I have to do
      > is to extract all the strings that begins with a "$" character, but
      > excluding characters like "." (point) and "'" (single quote) and "\" "/"
      > (slashes). For example I have:
      >
      > 1) This Is An $EXAMPLE String
      > 2) This Is An $EXAMPLE.String
      > 3) 'This Is An $EXAMPLE'
      > 4) This Is An \$EXAMPLE\Strin g;
      >
      > I would like to extract only the "keyword" $EXAMPLE and what I'm using at[/color]

      Is that what you want?
      [color=blue][color=green][color=darkred]
      >>> import re
      >>> r = re.compile("[$]\w+")
      >>> r.findall("""[/color][/color][/color]
      .... 1) This Is An $EXAMPLE String
      .... 2) This Is An $EXAMPLE.String
      .... 3) 'This Is An $EXAMPLE'
      .... 4) This Is An \$EXAMPLE\Strin g;
      .... """)
      ['$EXAMPLE', '$EXAMPLE', '$EXAMPLE', '$EXAMPLE']

      Peter

      Comment

      • Caleb Hattingh

        #4
        Re: Regular Expression Problem...

        Obviously, Peter and Jorge are hardcore, but below is what a beginner like
        me hacked up:

        My point, I guess, is that it is possible to quickly get a solution to a
        specific problem like this without being a total expert. The code below
        was typed out once, and with only one minor correction before it produced
        the required behaviour. Sure, it is not the general solution, but *this
        is WHY I use python* - You get a lot of mileage out of the basics. Plus,
        I can still read and understand the sub-optimal code, and probably will be
        able to several years from now :)

        Regular expressions are real powerful, and I am learning through using ViM
        - but it takes time and practice.

        (err, I didn't include your example with quotes, but you should get the
        general idea)
        ***
        '>>> stngs = [r'This Is An $EXAMPLE String', r'This Is An
        $EXAMPLE.String ', r'This Is An $EXAMPLE',r'Thi s Is An \$EXAMPLE\Strin g']
        '>>> stngs
        ['This Is An $EXAMPLE String', 'This Is An $EXAMPLE.String ', 'This Is An
        $EXAMPLE', 'This Is An \\$EXAMPLE\\Str ing']

        '>>> for i in stngs:
        wdlist = i.split(' ')
        for j in wdlist:
        if j.find(r'$') > -1:
        dwd = j.split('.')[0]
        if dwd.find('\\') > -1:
        dwd = dwd.split('\\')[1]
        print dwd

        $EXAMPLE
        $EXAMPLE
        $EXAMPLE
        $EXAMPLE
        '>>>
        ***




        On Wed, 01 Dec 2004 13:40:17 +0100, Peter Otten <__peter__@web. de> wrote:
        [color=blue]
        > andrea.gavana@a gip.it wrote:
        >[color=green]
        >> identifying/extracting a substring from another string. What I have to
        >> do
        >> is to extract all the strings that begins with a "$" character, but
        >> excluding characters like "." (point) and "'" (single quote) and "\" "/"
        >> (slashes). For example I have:
        >>
        >> 1) This Is An $EXAMPLE String
        >> 2) This Is An $EXAMPLE.String
        >> 3) 'This Is An $EXAMPLE'
        >> 4) This Is An \$EXAMPLE\Strin g;
        >>
        >> I would like to extract only the "keyword" $EXAMPLE and what I'm using
        >> at[/color]
        >
        > Is that what you want?
        >[color=green][color=darkred]
        >>>> import re
        >>>> r = re.compile("[$]\w+")
        >>>> r.findall("""[/color][/color]
        > ... 1) This Is An $EXAMPLE String
        > ... 2) This Is An $EXAMPLE.String
        > ... 3) 'This Is An $EXAMPLE'
        > ... 4) This Is An \$EXAMPLE\Strin g;
        > ... """)
        > ['$EXAMPLE', '$EXAMPLE', '$EXAMPLE', '$EXAMPLE']
        >
        > Peter
        >[/color]

        Comment

        Working...