using regexp

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

    #1

    using regexp

    hi
    how can i use regexp to group these digits into groups of 3?

    eg
    line 123456789123456 789

    i have :

    pat = re.compile("lin e\s+(\d{3})" , re.M|re.DOTALL)

    but this only gives the first 3. I also tried

    "line\s+(\d{3}) +"
    but also not working.
    I need output to be ['123' ,'456','789', '123','456','78 9', .....]
    thanks.

  • Shane Geiger

    #2
    Re: using regexp

    You don't even need regex.

    def
    split_seq(seq,s ize):

    # this is sort of the inverse of
    flatten

    # Source:


    return [seq[i:i+size] for i in range(0, len(seq),
    size)]



    line =
    '12345678912345 6789'



    print
    split_seq(line, 3)



    Will that work for you?



    s99999999s2003@ yahoo.com wrote:
    hi
    how can i use regexp to group these digits into groups of 3?
    >
    eg
    line 123456789123456 789
    >
    i have :
    >
    pat = re.compile("lin e\s+(\d{3})" , re.M|re.DOTALL)
    >
    but this only gives the first 3. I also tried
    >
    "line\s+(\d{3}) +"
    but also not working.
    I need output to be ['123' ,'456','789', '123','456','78 9', .....]
    thanks.
    >
    >
    --
    Shane Geiger
    IT Director
    National Council on Economic Education
    sgeiger@ncee.ne t | 402-438-8958 | http://www.ncee.net

    Leading the Campaign for Economic and Financial Literacy


    Comment

    • Shane Geiger

      #3
      Re: using regexp


      import re
      line = '12345678912345 6789'
      print re.findall('([0-9]{3})', line)



      Shane Geiger wrote:
      You don't even need regex.
      >
      def
      split_seq(seq,s ize):
      >
      # this is sort of the inverse of
      flatten
      >
      # Source:

      >
      return [seq[i:i+size] for i in range(0, len(seq),
      size)]
      >
      >
      >
      line =
      '12345678912345 6789'
      >
      >
      >
      print
      split_seq(line, 3)
      >
      >
      >
      Will that work for you?
      >
      >
      >
      s99999999s2003@ yahoo.com wrote:
      >hi
      >how can i use regexp to group these digits into groups of 3?
      >>
      >eg
      >line 123456789123456 789
      >>
      >i have :
      >>
      >pat = re.compile("lin e\s+(\d{3})" , re.M|re.DOTALL)
      >>
      >but this only gives the first 3. I also tried
      >>
      >"line\s+(\d{3} )+"
      >but also not working.
      >I need output to be ['123' ,'456','789', '123','456','78 9', .....]
      >thanks.
      >>
      >>
      >
      --
      Shane Geiger
      IT Director
      National Council on Economic Education
      sgeiger@ncee.ne t | 402-438-8958 | http://www.ncee.net

      Leading the Campaign for Economic and Financial Literacy


      Comment

      • s99999999s2003@yahoo.com

        #4
        Re: using regexp

        On Mar 20, 1:57 pm, Shane Geiger <sgei...@ncee.n etwrote:
        import re
        line = '12345678912345 6789'
        print re.findall('([0-9]{3})', line)
        >
        >
        >
        Shane Geiger wrote:
        You don't even need regex.
        >
        def
        split_seq(seq,s ize):
        >
        # this is sort of the inverse of
        flatten
        >>
        return [seq[i:i+size] for i in range(0, len(seq),
        size)]
        >
        line =
        '12345678912345 6789'
        >
        print
        split_seq(line, 3)
        >
        Will that work for you?
        >
        s99999999s2...@ yahoo.com wrote:
        hi
        how can i use regexp to group these digits into groups of 3?
        >
        eg
        line 123456789123456 789
        >
        i have :
        >
        pat = re.compile("lin e\s+(\d{3})" , re.M|re.DOTALL)
        >
        but this only gives the first 3. I also tried
        >
        "line\s+(\d{3}) +"
        but also not working.
        I need output to be ['123' ,'456','789', '123','456','78 9', .....]
        thanks.
        >
        --
        Shane Geiger
        IT Director
        National Council on Economic Education
        sgei...@ncee.ne t | 402-438-8958 | http://www.ncee.net
        >
        Leading the Campaign for Economic and Financial Literacy
        >
        sgeiger.vcf
        1KDownload

        hi
        thanks.
        I know it can be done without regexp, but its just for learning
        purpose, i want to use regexp
        eg
        someword 123456789123456 789 #this is a line of a file. 'someword' is a
        word before the digits
        I need to get the digits into groups of 3 based on whether i found
        'someword'.
        so i use this pattern :
        "someword\s+(\d {3})"
        but this will on give me the first 3. I need to group them all.
        hope i explain it clearly.

        Comment

        • attn.steven.kuo@gmail.com

          #5
          Re: using regexp

          On Mar 19, 10:33 pm, s99999999s2...@ yahoo.com wrote:
          hi
          how can i use regexp to group these digits into groups of 3?
          >
          eg
          line 123456789123456 789
          >
          i have :
          >
          pat = re.compile("lin e\s+(\d{3})" , re.M|re.DOTALL)
          >
          but this only gives the first 3. I also tried
          >
          "line\s+(\d{3}) +"
          but also not working.
          I need output to be ['123' ,'456','789', '123','456','78 9', .....]
          thanks.


          Try:

          import re

          target_string = """not 123456789 but
          try this line 987654321"""

          try:
          digits = re.compile(r'li ne\s+(\d
          +)').search(tar get_string).gro up(1)
          except AttributeError:
          digits = ''

          three_at_a_time = re.compile(r'\d {3}').findall(d igits)
          print three_at_a_time

          --
          Hope this helps,
          Steven

          Comment

          Working...