Simple regex with whitespaces

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mathieu.malaterre@gmail.com

    #1

    Simple regex with whitespaces

    Hello,

    I cannot figure out a way to find a regular expression that would
    match one and only one of these two strings:

    s1 = ' how are you'
    s2 = ' hello world how are you'

    All I could come up with was:
    patt = re.compile('^[ ]*([A-Za-z]+)[ ]+([A-Za-z]+)$')

    Which of course does not work. I cannot express the fact: sentence
    have 0 or 1 whitespace, separation of group have two or more
    whitespaces.

    Any suggestion ? Thanks a bunch !
    Mathieu

  • Mark Peters

    #2
    Re: Simple regex with whitespaces

    Which of course does not work. I cannot express the fact: sentence
    have 0 or 1 whitespace, separation of group have two or more
    whitespaces.
    >
    Any suggestion ? Thanks a bunch !
    How about this:
    >>import re
    >>s = ' hello world how are you'
    >>re.split(r"\s {2,}",s)
    ['', 'hello world', 'how are you']

    Comment

    • James Stroud

      #3
      Re: Simple regex with whitespaces

      mathieu.malater re@gmail.com wrote:
      Hello,
      >
      I cannot figure out a way to find a regular expression that would
      match one and only one of these two strings:
      >
      s1 = ' how are you'
      s2 = ' hello world how are you'
      >
      All I could come up with was:
      patt = re.compile('^[ ]*([A-Za-z]+)[ ]+([A-Za-z]+)$')
      >
      Which of course does not work. I cannot express the fact: sentence
      have 0 or 1 whitespace, separation of group have two or more
      whitespaces.
      >
      Any suggestion ? Thanks a bunch !
      Mathieu
      >

      pyimport re
      pys1 = ' how are you'
      pys2 = ' hello world how are you'
      pys3 = 'group here now here but not here but now here'
      pypatt_2plus = re.compile(r'(? :(?:\S+(?:\s|$) )+(?:\s+|$)){2, }')
      pypatt_3plus = re.compile(r'(? :(?:\S+(?:\s|$) )+(?:\s+|$)){3, }')

      positive tests:

      pypatt_2plus.se arch(s2).group( 0)
      'hello world how are you'
      pypatt_2plus.se arch(s3).group( 0)
      'group here now here but not here but now here'
      pypatt_3plus.se arch(s3).group( 0)
      'group here now here but not here but now here'



      negative tests:

      pypatt_3plus.se arch(s2).group( 0)
      Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      AttributeError: 'NoneType' object has no attribute 'group'
      pypatt_3plus.se arch(s1).group( 0)
      Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      AttributeError: 'NoneType' object has no attribute 'group'
      pypatt_2plus.se arch(s1).group( 0)
      Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      AttributeError: 'NoneType' object has no attribute 'group'



      James

      --
      James Stroud
      UCLA-DOE Institute for Genomics and Proteomics
      Box 951570
      Los Angeles, CA 90095


      Comment

      • John Machin

        #4
        Re: Simple regex with whitespaces

        mathieu.malater re@gmail.com wrote:
        Hello,
        >
        I cannot figure out a way to find a regular expression that would
        match one and only one of these two strings:
        >
        s1 = ' how are you'
        s2 = ' hello world how are you'
        >
        All I could come up with was:
        patt = re.compile('^[ ]*([A-Za-z]+)[ ]+([A-Za-z]+)$')
        >
        Which of course does not work. I cannot express the fact: sentence
        have 0 or 1 whitespace, separation of group have two or more
        whitespaces.
        >
        Any suggestion ? Thanks a bunch !
        Mathieu
        1. A "word" is one or more non-whitespace charaters -- subpattern is
        \S+
        2. A "sentence" is one or more words separated by a single white space
        IOW a word followed by zero or more occurrences of whitespace+word --
        so a sentence will be matched by \S+(\s\S+)*
        3. Leading and trailing runs of whitespace should be ignored -- use \s*
        4. You will need to detect the case of 0 sentences (all whitespace)
        separately -- I trust you don't need to be told how to do that :-)
        5. Don't try to match two or more sentences; match one sentence, and
        anything that fails must 0 or 2+ sentences.

        So :

        |>>s1 = ' how are you'
        |>>s2 = ' hello world how are you'
        |>>pat = r"^\s*\S+(\s\S+ )*\s*$"
        |>>import re
        |>>re.match(pat , s1)
        |<_sre.SRE_Matc h object at 0x00AED9E0>
        |>>re.match(pat , s2)
        |>>re.match(pat , ' ')
        |>>re.match(pat , ' a b ')
        |>>re.match(pat , ' a b ')
        |<_sre.SRE_Matc h object at 0x00AED8E0>
        |>>re.match(pat , ' ab ')
        |<_sre.SRE_Matc h object at 0x00AED920>
        |>>re.match(pat , ' a ')
        |<_sre.SRE_Matc h object at 0x00AED9E0>
        |>>re.match(pat , 'a')
        |<_sre.SRE_Matc h object at 0x00AED8E0>
        |>>>

        HTH,
        John

        Comment

        • Paul McGuire

          #5
          Re: Simple regex with whitespaces

          <mathieu.malate rre@gmail.comwr ote in message
          news:1157945804 .805624.32620@p 79g2000cwp.goog legroups.com...
          Hello,
          >
          I cannot figure out a way to find a regular expression that would
          match one and only one of these two strings:
          >
          s1 = ' how are you'
          s2 = ' hello world how are you'
          >
          All I could come up with was:
          patt = re.compile('^[ ]*([A-Za-z]+)[ ]+([A-Za-z]+)$')
          >
          Which of course does not work. I cannot express the fact: sentence
          have 0 or 1 whitespace, separation of group have two or more
          whitespaces.
          >
          Any suggestion ? Thanks a bunch !
          Mathieu
          >
          A pyparsing approach is not as terse as regexp's, but it's not terribly long
          either. Following John Machin's submission as a pattern:

          s1 = ' how are you'
          s2 = ' hello world how are you'

          from pyparsing import *

          wd = Word(printables )
          # this is necessary to suppress pyparsing's built-in whitespace skipping
          wd.leaveWhitesp ace()
          sentence = delimitedList(w d, delim=White(' ',exact=1))

          for test in (s1,s2):
          print sentence.search String(test)


          Pyparsing returns data as ParseResults objects, which can be accessed as
          lists or dicts. From this first cut, we get:
          [['how', 'are', 'you']]
          [['hello', 'world'], ['how', 'are', 'you']]

          These aren't really sentences any more, but we can have pyparsing put them
          back into sentences, by adding a parse action to sentence.

          sentence.setPar seAction(lambda toks: " ".join(toks ))

          Now our results are:
          [['how are you']]
          [['hello world'], ['how are you']]


          If you really want to get fancy, and clean up some of that capitalization
          and lack of punctuation, you can add a more elaborate parse action instead:
          ispunc = lambda s: s in ".!?;:,"
          sixLoyalServing Men = ('What','Why',' When','How','Wh ere','Who')
          def cleanup(t):
          t[0] = t[0].title()
          if not ispunc( t[-1][-1] ):
          if t[0] in sixLoyalServing Men:
          punc = "?"
          else:
          punc = "."
          else:
          punc = ""
          return " ".join(t) + punc
          sentence.setPar seAction(cleanu p)

          This time we get:
          [['How are you?']]
          [['Hello world.'], ['How are you?']]


          The pyparsing home page is at pyparsing.wikis paces.com.

          -- Paul


          Comment

          • James Stroud

            #6
            Re: Simple regex with whitespaces

            Paul McGuire wrote:
            <mathieu.malate rre@gmail.comwr ote in message
            news:1157945804 .805624.32620@p 79g2000cwp.goog legroups.com...
            >
            >>Hello,
            >>
            >I cannot figure out a way to find a regular expression that would
            >>match one and only one of these two strings:
            [clip]
            >>Any suggestion ? Thanks a bunch !
            >>Mathieu
            >>
            >
            A pyparsing approach is not as terse as regexp's, but it's not terribly long
            either.
            To second Paul's suggestion, usually, if the regex is not immediately
            obvious, its probably better to look into parsing modules, pyparsing
            being one of the most accessible modules (to me, anyways)--and well
            worth learning.

            However, in complicated applications, regex is usually still fun and
            valuable as an intellectual exercise.

            James

            --
            James Stroud
            UCLA-DOE Institute for Genomics and Proteomics
            Box 951570
            Los Angeles, CA 90095


            Comment

            • John Machin

              #7
              Re: Simple regex with whitespaces


              James Stroud wrote:
              Paul McGuire wrote:
              <mathieu.malate rre@gmail.comwr ote in message
              news:1157945804 .805624.32620@p 79g2000cwp.goog legroups.com...
              >Hello,
              >
              I cannot figure out a way to find a regular expression that would
              >match one and only one of these two strings:
              [clip]
              >Any suggestion ? Thanks a bunch !
              >Mathieu
              >
              A pyparsing approach is not as terse as regexp's, but it's not terribly long
              either.
              >
              To second Paul's suggestion, usually, if the regex is not immediately
              obvious, its probably better to look into parsing modules, pyparsing
              being one of the most accessible modules (to me, anyways)--and well
              worth learning.
              I would say that it's better, before leaping to the implementation, to
              understand the problem. IOW, rough out the grammar first --- in the
              current case, not very complicated at all; the way I looked at it there
              are only two possible outcomes from the tokeniser (space and not-space)
              and only three non-terminal symbols (word, sentence, paragraph) -- then
              choose the implementation.
              >
              However, in complicated applications, regex is usually still fun and
              valuable as an intellectual exercise.
              Indeed. Better use of the mind and the CPU than distractions du jour
              like pseudorubiku or whatever it's called :-)

              Cheers,
              John

              Comment

              Working...