-2

I cannot print components of matched regex.

I am learning python3 and I need to verify that output of my command matches my needs. I have following short code:

#!/usr/bin/python3

import re

text_to_search = ''' 
   1 | 27  23   8 |
   2 | 21  23   8 |
   3 | 21  23   8 |
   4 | 21  21  21 |
   5 | 21  21  21 |
   6 | 27  27  27 |
   7 | 27  27  27 |
'''

pattern = re.compile('(.*\n)*(   \d \| 2[17]  2[137]  [ 2][178] \|)')
matches = pattern.finditer(text_to_search)

for match in matches:
    print (match)
    print ()
    print ('matched to group 0:' + match.group(0))
    print ()
    print ('matched to group 1:' + match.group(1))
    print ()
    print ('matched to group 2:' + match.group(2))

and following output:

<_sre.SRE_Match object; span=(0, 140), match='\n   1 | 27  23   8 |\n   2 | 21  23   8 |\n   3 >

matched to group 0:
   1 | 27  23   8 |
   2 | 21  23   8 |
   3 | 21  23   8 |
   4 | 21  21  21 |
   5 | 21  21  21 |
   6 | 27  27  27 |
   7 | 27  27  27 |

matched to group 1:   6 | 27  27  27 |


matched to group 2:   7 | 27  27  27 |

please explain me:
1) why "print (match)" prints only beginning of match, does it have some kind of limit to trim output if its bigger than some threshold?
2) Why group(1) is printed as "6 | 27 27 27 |" ? I was hope (.*\n)* is as greedy as possible so it consumes everything from 1-6 lines, leaving last line of text_to_search to be matched against group(2), but seems (.*\n)* took only 6-th line. Why is that? Why lines 1-5 are not printed when printing group(1)?
3) I was trying to go through regex tutorial but failed to understand those tricks with (?...). How do I verify if numbers in last row are equal (so 27 27 27 is ok, but 21 27 27 is not)?

  • What is it exactly you are trying to get? each row as a group? As to your second point: A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations – Tomerikoo Jul 13 '19 at 14:28
  • 1
    Have you tried *analyzing* your pattern with any of the online regex testers/tools? – wwii Jul 13 '19 at 14:32
  • You should probably ask three separate questions - this isn't a discussion forum or tutorial. Question 2 probably has a duplicate here on SO. – wwii Jul 13 '19 at 14:35
  • Possible duplicate of [How do I repeat a capturing group?](https://stackoverflow.com/questions/28318016/how-do-i-repeat-a-capturing-group) – wwii Jul 13 '19 at 14:37
  • also for q2: [regexp group repetition in python](https://stackoverflow.com/questions/23669872/regexp-group-repetition-in-python)] – wwii Jul 13 '19 at 14:43
  • [Can I ask only one question per post?](https://meta.stackexchange.com/questions/222735/can-i-ask-only-one-question-per-post) – wwii Jul 13 '19 at 14:45

1 Answers1

1

1) The print(match) only shows an outline of the object. match is an SRE_Match object, so in order to get information from it you need to do something like match.group(0), which is accessing a value stored in the object.

2) to capture lines 1-6 you need to change (.*\n)* to ((?:.*\n)*) according to this regex tester,

A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data

3) to match specific numbers you need to make it more specific and include these numbers into a seperate group at the end.

Noah Cristino
  • 757
  • 8
  • 29
  • Thx for answer for 1 and 2.
    I do not want to have specific numbers. I want to be sure that first, second, and last number are equal. In last row It doesnt matter what value it is, only equality matters for me.
    – dark.andre.c Jul 14 '19 at 21:29