Re: regex and newlines

Guido.van.Rossum@cwi.nl
Fri, 06 Jan 1995 03:03:14 +0100

> I'm having a problem with regex stopping a generic character match at a
> newline. For example, the following code will print "1" and not "1\n2"
>
> import regex
>
> r = regex.compile("image \(.*\)")
> r.search("image 1\n2")
> print r.group(1)
>
> Is there a flag I can set so .* will include the newline in the match?

No, the meaning of '.' as matching anything except \n has been
hardwired in the Emacs (-compatible) regular expression matching
package that I am using.

However, since this package (like all of Python where not constrained
by external factors) is 8-bit clean and supports null bytes in strings
and patterns, you can use the following instead of ".*" to match
absolutely everything: "[\0-\377]*". (Note that the \ octal escapes
are expanded by the string literal, not by the regex module.)

Hope this helps,

--Guido van Rossum, CWI, Amsterdam <mailto:Guido.van.Rossum@cwi.nl>
<http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>