regex help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lance Hoffmeyer

    #1

    regex help

    I have the following table and I am trying to match percentage the 2nd column on the 2nd Tiger line (9.0).

    I have tried both of the following. I expected both to match but neither did? Is there a modifier
    I am missing? What changes do I need to make these match? I need to keep the structure of the regex
    the same.

    TIGER.append(re .search("TIGER\ s{10}.*?(?:(\d{ 1,3}\.\d)\s+){2 }", target_table).g roup(1))
    TIGER.append(re .search("^TIGER .*?(?:(\d{1,3}\ .\d)\s+){2}", target_table).g roup(1))


    BASE - TOTAL TIGER 268 268 173 95 101 - 101 57 - 57 78 2 76 268 192 76 230 21

    DOG 79 79 44 35 31 - 31 17 - 17 25 1 24 79 55 24 75 1
    29.5 29.5 25.4 36.8 30.7 - 30.7 29.8 - 29.8 32.1 50.0 31.6 29.5 28.6 31.6 32.6 4.8

    CAT 46 46 28 18 20 - 20 7 - 7 14 - 14 46 32 14 39 4
    17.2 17.2 16.2 18.9 19.8 - 19.8 12.3 - 12.3 17.9 - 18.4 17.2 16.7 18.4 17.0 19.0

    LAMB 32 32 23 9 10 - 10 8 - 8 12 - 12 32 20 12 28 1
    11.9 11.9 13.3 9.5 9.9 - 9.9 14.0 - 14.0 15.4 - 15.8 11.9 10.4 15.8 12.2 4.8

    TRIPOD 32 32 23 9 9 - 9 9 - 9 11 1 10 32 22 10 28 3
    11.9 11.9 13.3 9.5 8.9 - 8.9 15.8 - 15.8 14.1 50.0 13.2 11.9 11.5 13.2 12.2 14.3

    TIGER 24 24 16 8 5 - 5 10 - 10 7 - 7 24 17 7 18 2
    9.0 9.0 9.2 8.4 5.0 - 5.0 17.5 - 17.5 9.0 - 9.2 9.0 8.9 9.2 7.8 9.5
  • Peter Otten

    #2
    Re: regex help

    Lance Hoffmeyer wrote:
    [color=blue]
    > I have the following table and I am trying to match percentage the 2nd
    > column on the 2nd Tiger line (9.0).
    >
    > I have tried both of the following. I expected both to match but neither
    > did? Is there a modifier
    > I am missing? What changes do I need to make these match? I need to keep
    > the structure of the regex the same.
    >
    > TIGER.append(re .search("TIGER\ s{10}.*?(?:(\d{ 1,3}\.\d)\s+){2 }",
    > target_table).g roup(1))
    > TIGER.append(re .search("^TIGER .*?(?:(\d{1,3}\ .\d)\s+){2}",
    > target_table).g roup(1))[/color]

    You can try the re.DOTALL flag (prepend the regex string with "(?s)"), but
    I'd go with something really simple:

    instream = iter(target_tab le.splitlines() ) # or: instream = open(datafile)
    for line in instream:
    if line.startswith ("TIGER"):
    value = instream.next() .split()[1] # or ...[0]? they are both '9.0'
    TIGER.append(va lue)
    break

    Peter

    Comment

    • johnzenger@gmail.com

      #3
      Re: regex help

      Why not use split instead of regular expressions?
      [color=blue][color=green][color=darkred]
      >>> ln = "32 32 23 9 9 - 9 9 - 9 11 1 10"
      >>> ln.split()[/color][/color][/color]
      ['32', '32', '23', '9', '9', '-', '9', '9', '-', '9', '11', '1', '10']

      Much simpler, yes? Just find the line that comes after a line that
      begins with "TIGER," split it, and pick the number you want out of the
      resulting list.

      Lance Hoffmeyer wrote:[color=blue]
      > I have the following table and I am trying to match percentage the 2nd column on the 2nd Tiger line (9.0).
      >
      > I have tried both of the following. I expected both to match but neither did? Is there a modifier
      > I am missing? What changes do I need to make these match? I need to keep the structure of the regex
      > the same.
      >
      > TIGER.append(re .search("TIGER\ s{10}.*?(?:(\d{ 1,3}\.\d)\s+){2 }", target_table).g roup(1))
      > TIGER.append(re .search("^TIGER .*?(?:(\d{1,3}\ .\d)\s+){2}", target_table).g roup(1))
      >
      >
      > BASE - TOTAL TIGER 268 268 173 95 101 - 101 57 - 57 78 2 76 268 192 76 230 21
      >
      > DOG 79 79 44 35 31 - 31 17 - 17 25 1 24 79 55 24 75 1
      > 29.5 29.5 25.4 36.8 30.7 - 30.7 29.8 - 29.8 32.1 50.0 31.6 29.5 28.6 31.6 32.6 4.8
      >
      > CAT 46 46 28 18 20 - 20 7 - 7 14 - 14 46 32 14 39 4
      > 17.2 17.2 16.2 18.9 19.8 - 19.8 12.3 - 12.3 17.9 - 18.4 17.2 16.7 18.4 17.0 19.0
      >
      > LAMB 32 32 23 9 10 - 10 8 - 8 12 - 12 32 20 12 28 1
      > 11.9 11.9 13.3 9.5 9.9 - 9.9 14.0 - 14.0 15.4 - 15.8 11.9 10.4 15.8 12.2 4.8
      >
      > TRIPOD 32 32 23 9 9 - 9 9 - 9 11 1 10 32 22 10 28 3
      > 11.9 11.9 13.3 9.5 8.9 - 8.9 15.8 - 15.8 14.1 50.0 13.2 11.9 11.5 13.2 12.2 14.3
      >
      > TIGER 24 24 16 8 5 - 5 10 - 10 7 - 7 24 17 7 18 2
      > 9.0 9.0 9.2 8.4 5.0 - 5.0 17.5 - 17.5 9.0 - 9.2 9.0 8.9 9.2 7.8 9.5[/color]

      Comment

      Working...