- #1
- 29,352
- 4,097
I don't know python, yet I am trying to code something, just for fun. But I have hit some strange obstacle and I don't know if it is a bug in my Python version (dated, it is over a year old, under Raspbian), or is it me misunderstanding something?
Output:
Why is the output using m.group() strings garbled? Strings are matched correctly, m.group() returns a string, so it should work the same way it works in the last output line - but it doesn't. I understand [] can be used to access arguments’ items, but I don't see a way of escaping them, besides, it doesn't seem to be a problem in other cases.
The simplest thing to try would be to upgrade to the newest python version, but I am unable to. I get message that some files are missing and suggestion to do full update - which I don't want to try, as I was not able to setup my WiFi dongle on another Pi with the newest Raspbian.
Code:
pattern = re.compile('\[(\d+)\]\[(\d+)\](.*)')
line = '[123][456]789'
print 'original string:'
print line
m = pattern.match(line)
print 'matched substrings:'
print m.groups()
fst = '[{}][{}]{}'
print 'output using strings returned from m.group():'
print fst.format(m.group(0), m.group(1), m.group(2))
print 'output using just strings:'
print fst.format('123','456','789')
Output:
Code:
original string:
[123][456]789
matched substrings:
('123', '456', '789')
output using strings returned from m.group():
[[123][456]789][123]456
output using just strings:
[123][456]789
Why is the output using m.group() strings garbled? Strings are matched correctly, m.group() returns a string, so it should work the same way it works in the last output line - but it doesn't. I understand [] can be used to access arguments’ items, but I don't see a way of escaping them, besides, it doesn't seem to be a problem in other cases.
The simplest thing to try would be to upgrade to the newest python version, but I am unable to. I get message that some files are missing and suggestion to do full update - which I don't want to try, as I was not able to setup my WiFi dongle on another Pi with the newest Raspbian.