Splitting a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nico Grubert

    #1

    Splitting a string

    Dear Python users,

    I'd like to split a string where 'and', 'or', 'and not' occurs.

    Example string:
    s = 'Smith, R. OR White OR Blue, T. AND Black AND Red AND NOT Green'

    I need to split s in order to get this list:
    ['Smith, R.', 'White', 'Blue, T.', 'Back', 'Red', 'Green']

    Any idea, how I can split a string where 'and', 'or', 'and not' occurs?


    Thank you very much in advance,
    Nico
  • Dylan Moreland

    #2
    Re: Splitting a string

    Take a look at:



    So I would try something like:

    pat = re.compile(r" (?:AND|OR|AND NOT) ")
    pat.split(strin g)

    Compile the regular expression with re.IGNORECASE if you like.

    Nico Grubert wrote:[color=blue]
    > Dear Python users,
    >
    > I'd like to split a string where 'and', 'or', 'and not' occurs.
    >
    > Example string:
    > s = 'Smith, R. OR White OR Blue, T. AND Black AND Red AND NOT Green'
    >
    > I need to split s in order to get this list:
    > ['Smith, R.', 'White', 'Blue, T.', 'Back', 'Red', 'Green']
    >
    > Any idea, how I can split a string where 'and', 'or', 'and not' occurs?
    >
    >
    > Thank you very much in advance,
    > Nico[/color]

    Comment

    • Fredrik Lundh

      #3
      Re: Splitting a string

      Dylan Moreland wrote:
      [color=blue]
      > So I would try something like:
      >
      > pat = re.compile(r" (?:AND|OR|AND NOT) ")
      > pat.split(strin g)[/color]

      footnote: this yields:

      ['Smith, R.', 'White', 'Blue, T.', 'Black', 'Red', 'NOT Green']

      (the | operator picks the first (leftmost) alternative that results in an
      overall match.)

      </F>



      Comment

      • Dylan Moreland

        #4
        Re: Splitting a string

        Woops! Thanks for the correction. I was assuming greediness for some
        reason.

        Fredrik Lundh wrote:[color=blue]
        > Dylan Moreland wrote:
        >[color=green]
        > > So I would try something like:
        > >
        > > pat = re.compile(r" (?:AND|OR|AND NOT) ")
        > > pat.split(strin g)[/color]
        >
        > footnote: this yields:
        >
        > ['Smith, R.', 'White', 'Blue, T.', 'Black', 'Red', 'NOT Green']
        >
        > (the | operator picks the first (leftmost) alternative that results in an
        > overall match.)
        >
        > </F>[/color]

        Comment

        • Paul Rubin

          #5
          Re: Splitting a string

          Nico Grubert <nicogrubert@gm ail.com> writes:[color=blue]
          > I'd like to split a string where 'and', 'or', 'and not' occurs.[/color]

          Other people have suggested how to do this splitting. But don't you
          really want a parser?

          Comment

          • Christoph Zwerschke

            #6
            Re: Splitting a string

            Nico Grubert wrote:
            [color=blue]
            > I'd like to split a string where 'and', 'or', 'and not' occurs.
            > Example string:
            > s = 'Smith, R. OR White OR Blue, T. AND Black AND Red AND NOT Green'[/color]

            Here is a solution without using the re module:
            s.replace(' AND NOT ', ' OR ').replace(' AND ', ' OR ').split(' OR ')

            -- Christoph

            Comment

            Working...