regular expressions with other string classes

Nicholas FitzRoy-Dale wzdd at lardcave.net
Wed Sep 19 09:55:44 CEST 2001


On 19 Sep, Andrew Dalke wrote:
> Juan Valiño:
>>It is possible to use the regular expression engine with other string-like
>>structures?.
> 
> Sometimes.  They work on 'array's
> 
> Python 2.2a3+ (#6, Sep 17 2001, 05:05:24)
> [GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import re
>>>> pat = re.compile("^(.*(?=dalke).*)$", re.MULTILINE)
>>>>
> 
> 
>>>> import array
>>>> s = array.array("c", open("/etc/passwd").read())
>>>> m = pat.search(s)
>>>> m
> <SRE_Match object at 0x1202d1430>
>>>> m.group(1)
> array('c', 'dalke:*:100:100:Andrew Dalke:/home/dalke:/bin/tcsh')
>>>>
> 
> but they don't seem to work with memory mapped files.

I'm doing regular expression search on an MMAPed file without issues.
>From code to index an mbox-style file:

matchRE = re.compile (NEWMAILMATCHER)
handle = open (self.sourceFilename, 'r')
mboxMap = mmap.mmap (handle.fileno(), getFileLength (self.sourceFilename),
	mmap.MAP_SHARED, mmap.PROT_READ)
# Get the first email start.
matchObj = matchRE.search (mboxMap, 0)
# Get the next email start.
matchObj = matchRE.search (mboxMap, matchObj.end())
-- 
- Nicholas FitzRoy-Dale
http://www.lardcave.net

 /. signature:
"Hit a man on the head with a fish, and he'll have a headache for a day..."





More information about the Python-list mailing list