python3 regex?

Christian Gollwitzer auriocus at gmx.de
Sat Sep 10 01:30:29 EDT 2016


Am 10.09.16 um 05:12 schrieb dkoleary at olearycomputers.com:
> Hey;
>
> Long term perl ahderent finally making the leap to python.  From my reading, python, for the most part, uses perl regex.. except, I can't seem to make it work...
>
> I have a txt file from which I can grab specific titles via a perl one-liner:
>
> $ perl -ne 'print if (m{^("?)[1-9]*\.})' tables
> 1. ${title1}
> 2. ${title2}
> "3. ${title3}",,,
> 4. one more title
> 5. nuther title
> 6. and so on...,,
> ...
> 25. last title
>
> I can't seem to get the same titles to appear using python:
>
>
> $ python -V
> Python 3.5.2
> $ python
> Python 3.5.2 (default, Jul  5 2016, 12:43:10)
> [GCC 5.4.0 20160609] on linux
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import os
>>>> import re
>>>> with open('tables','rt') as f:
> 	data = f.read()
>
> printing data results in the output I would expect..
>
> Trying to compile a regex and display the results does not show the same results I get from perl.
>
>>>> regex = r'^("?)[1-9]*\.'
>>>> re.search(regex, data)
>>>>
>
>>>> p = re.compile(r'^("?)[1-9]*\.')
>>>> p
> re.compile('^("?)[1-9]*\\.')
>>>> p.findall(data)
>
> I've tried any number of options shown on the net all with the same result.
> Can someone point out what I'm messing up?

I think the difference is the anchor ^. In perl, you apply the regex to 
each line, whereas in Python you apply it to the whole file. You would 
either need to switch the re to multiline mode
https://docs.python.org/2/library/re.html#re.MULTILINE

or do it in a loop, the same way that perl does it implicitly:

for line in f:
	print(re.findall(line).group(0))

(untested)

	Christian




More information about the Python-list mailing list