regexp qns

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • eight02645999@yahoo.com

    #1

    regexp qns

    hi
    suppose i have a string like

    test1?test2t-test3*test4*tes t5$test6#test7* test8

    how can i construct the regexp to get test3*test4*tes t5 and
    test7*test8, ie, i want to match * and the words before and after?
    thanks

  • James Stroud

    #2
    Re: regexp qns

    eight02645999@y ahoo.com wrote:
    hi
    suppose i have a string like
    >
    test1?test2t-test3*test4*tes t5$test6#test7* test8
    >
    how can i construct the regexp to get test3*test4*tes t5 and
    test7*test8, ie, i want to match * and the words before and after?
    thanks
    >

    pyimport re
    pys = 'test1?test2t-test3*test4*tes t5$test6#test7* test8'
    pyr = re.compile(r'(t est\d(?:\*test\ d)+)')
    pyr.findall(s)
    ['test3*test4*te st5', 'test7*test8']

    James

    Comment

    • eight02645999@yahoo.com

      #3
      Re: regexp qns

      James Stroud wrote:
      eight02645999@y ahoo.com wrote:
      hi
      suppose i have a string like

      test1?test2t-test3*test4*tes t5$test6#test7* test8

      how can i construct the regexp to get test3*test4*tes t5 and
      test7*test8, ie, i want to match * and the words before and after?
      thanks
      >
      >
      pyimport re
      pys = 'test1?test2t-test3*test4*tes t5$test6#test7* test8'
      pyr = re.compile(r'(t est\d(?:\*test\ d)+)')
      pyr.findall(s)
      ['test3*test4*te st5', 'test7*test8']
      >
      James
      thanks !
      I check the regexp doc it says:
      """
      (?:...)
      A non-grouping version of regular parentheses. Matches whatever
      regular expression is inside the parentheses, but the substring matched
      by the group cannot be retrieved after performing a match or referenced
      later in the pattern.
      """
      but i could not understand this : r'(test\d(?:\*t est\d)+)'. which
      parenthesis is it referring to? Sorry, could you explain the solution ?
      thanks

      Comment

      • Gabriel Genellina

        #4
        Re: regexp qns


        <eight02645999@ yahoo.comescrib ió en el mensaje
        news:1169268024 .164643.71320@l 53g2000cwa.goog legroups.com...
        hi
        suppose i have a string like
        >
        test1?test2t-test3*test4*tes t5$test6#test7* test8
        >
        how can i construct the regexp to get test3*test4*tes t5 and
        test7*test8, ie, i want to match * and the words before and after?
        thanks
        I suppose this is just an example and you mean "any word" instead of test1,
        test2, etc.
        So your pattern would be: word*word*word* word, that is, word* repeated many
        times, followed by another word.
        To match a word we'll use "\w+", to match an * we have to use "\*" (it's a
        special character)
        So the regexp would be: "(\w+\*)+\w +"
        Since we are not interested in the () as a group by itself -it was just to
        describe the repeating pattern- we change it into a non-grouping
        parenthesis.
        Final version: "(?:\w+\*)+ \w+"

        import re
        rexp = re.compile(r"(? :\w+\*)+\w+")
        lines = [
        'test1?test2t-test3*test4*tes t5$test6#test7* test8',
        'test1?test2t-test3*test4$tes t6#test7_test8' ,
        'test1?nada-que-ver$esto.no.mat chea',
        'test1?test2t-test3*test4*',
        'test1?test2t-test3*test4',
        'test1?test2t-test3*',
        ]

        for line in lines:
        print line
        for txt in rexp.findall(li ne):
        print '->', txt

        Test it with some corner cases and see if it does what you expect: no "*",
        starting with "*", ending with "*", embedded whitespace before and after the
        "*", whitespace inside a word, the very definition of "word"...

        --
        Gabriel Genellina


        Comment

        • James Stroud

          #5
          Re: regexp qns

          eight02645999@y ahoo.com wrote:
          James Stroud wrote:
          >
          >>eight02645999 @yahoo.com wrote:
          >>
          >>>hi
          >>>suppose i have a string like
          >>>
          >>>test1?test 2t-test3*test4*tes t5$test6#test7* test8
          >>>
          >>>how can i construct the regexp to get test3*test4*tes t5 and
          >>>test7*test 8, ie, i want to match * and the words before and after?
          >>>thanks
          >>>
          >>
          >>
          >>pyimport re
          >>pys = 'test1?test2t-test3*test4*tes t5$test6#test7* test8'
          >>pyr = re.compile(r'(t est\d(?:\*test\ d)+)')
          >>pyr.findall(s )
          >>['test3*test4*te st5', 'test7*test8']
          >>
          >>James
          >
          >
          thanks !
          I check the regexp doc it says:
          """
          (?:...)
          A non-grouping version of regular parentheses. Matches whatever
          regular expression is inside the parentheses, but the substring matched
          by the group cannot be retrieved after performing a match or referenced
          later in the pattern.
          """
          but i could not understand this : r'(test\d(?:\*t est\d)+)'. which
          parenthesis is it referring to? Sorry, could you explain the solution ?
          thanks
          >
          The outer parentheses are the grouping operator. These are saved and
          accessible from a match object via group() or groups() methods. The "\d"
          part matches a single digit 0-1. The (?:....) construct is used to make
          a non-grouping operator that is not itself remembered for access through
          the group() or groups() methods. The expression can also reference
          earlier groups, but not groups specified with the non-grouping operator.

          You may want to note that this is the most specific regular expression
          that would match your given example.

          James

          Comment

          Working...