Regular Expressions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    Regular Expressions

    Hey,

    I have a string

    s =740446505 "O'HANLON, Benjamin"

    i need to use one RE to convert it into

    (740446505, "O'HANLN", "Benjamin") ,

    I am using python 2.3 and have been stuydin the various tutotrials and the
    howto websites for the past 3 days and haven't managed to find a solution.

    Any help to get to the solution will be very very helpful.

    Thanks in advance for all the help.

    Achal Jalan


    ---------------------------------
    Do you Yahoo!?
    Yahoo! Finance Tax Center - File online. File on time.
  • Ed Suominen

    #2
    Re: Regular Expressions

    m = sre.search('(\d +)\s+\"[\w\'][,\s]+([\w\'])', s)
    x = (int(m.group(1) ), group(2), group(3))

    - Ed Suominen

    achaljalan@yaho o.com wrote:
    [color=blue]
    > Hey,
    >
    > I have a string
    >
    > s =740446505 "O'HANLON, Benjamin"
    >
    > i need to use one RE to convert it into
    >
    > (740446505, "O'HANLN", "Benjamin") ,
    >
    > I am using python 2.3 and have been stuydin the various tutotrials and the
    > howto websites for the past 3 days and haven't managed to find a solution.
    >
    > Any help to get to the solution will be very very helpful.
    >
    > Thanks in advance for all the help.
    >
    > Achal Jalan
    >
    >
    > ---------------------------------
    > Do you Yahoo!?
    > Yahoo! Finance Tax Center - File online. File on time.[/color]

    Comment

    • Ed Suominen

      #3
      Re: Regular Expressions

      Sorry, the RE should be '(\d+)\s+\"([\w\']+)[,\s]+([\w\']+)'

      Ed Suominen wrote:
      [color=blue]
      > m = sre.search('(\d +)\s+\"[\w\'][,\s]+([\w\'])', s)
      > x = (int(m.group(1) ), group(2), group(3))
      >
      > - Ed Suominen
      >
      > achaljalan@yaho o.com wrote:
      >[color=green]
      >> Hey,
      >>
      >> I have a string
      >>
      >> s =740446505 "O'HANLON, Benjamin"
      >>
      >> i need to use one RE to convert it into
      >>
      >> (740446505, "O'HANLN", "Benjamin") ,
      >>
      >> I am using python 2.3 and have been stuydin the various tutotrials and
      >> the howto websites for the past 3 days and haven't managed to find a
      >> solution.
      >>
      >> Any help to get to the solution will be very very helpful.
      >>
      >> Thanks in advance for all the help.
      >>
      >> Achal Jalan
      >>
      >>
      >> ---------------------------------
      >> Do you Yahoo!?
      >> Yahoo! Finance Tax Center - File online. File on time.[/color][/color]

      Comment

      • Cameron Laird

        #4
        Re: Regular Expressions

        In article <76adncn7cvNZl_ vdRVn-jA@centurytel.n et>,
        Ed Suominen <ed-no@spam-eepatents.com> wrote:[color=blue]
        >Sorry, the RE should be '(\d+)\s+\"([\w\']+)[,\s]+([\w\']+)'
        >
        >Ed Suominen wrote:
        >[color=green]
        >> m = sre.search('(\d +)\s+\"[\w\'][,\s]+([\w\'])', s)
        >> x = (int(m.group(1) ), group(2), group(3))[/color][/color]

        Comment

        • Eddie Corns

          #5
          Re: Regular Expressions

          <achaljalan@yah oo.com> writes:
          [color=blue]
          >--0-1311302120-1080428097=:611 25
          >Content-Type: text/plain; charset=us-ascii[/color]
          [color=blue]
          >Hey,[/color]
          [color=blue]
          >I have a string
          > s =740446505 "O'HANLON, Benjamin"
          >i need to use one RE to convert it into
          >
          > (740446505, "O'HANLN", "Benjamin") ,[/color]

          What you need to tell us is what the input *pattern* is eg

          s =NNNNNNNNN "Surname, Forename"

          what are the rules? are those quotes always there? can you guarantee a single
          comma? (ie no initials), do you always strip the second O from the Surname? :)

          It actually looks suspiciously like a fixed format string which would be much
          easier to break up using string slicing and string routines like split(',')
          and find('"') etc.

          Have we had the following quote on c.l.p recently:

          Some people, when confronted with a problem, think "I know, I'll use
          regular expressions." Now they have two problems.

          --Jamie Zawinski, in comp.lang.emacs

          Seeing this the first time did actually make me realise I was probably using
          REs too often.

          Eddie

          Comment

          Working...