What RE would i use to match this whole word?

G. Willoughby thecalm at NOSPAM.btinternet.com
Sun Jul 29 08:50:52 EDT 2001


this is the code im using for a 'file file' kinda program:

[snip]

def search(resultPane, dir, files):
    searchString = re.compile(str(entryField.get()), re.IGNORECASE) # Get
search string
    for file in files:
        if searchString.search(file) == "None":
            calculateResultSize()
            resultPane.update()
        else:
            resultPane.insert(END, os.path.join(dir, file))
            calculateResultSize()
            resultPane.update()

def go():
    if str(entryField.get()) == "":
        status.set("No search string has been entered!")
    else:
        resultPane.delete(0, END) # Clear the results pane!
        driveList=string.split(win32api.GetLogicalDriveStrings(),'\0')[:-1]
# Get Drives
        for x in range(0,len(driveList)):
            os.path.walk(driveList[x], search, resultPane)

[snip]

when the search is started the string which is taken from the entry field on
the form it is passed to the re.compile() function and an os.path.walk is
started. but this matches everything! in other words any file that contains
any of the characters from the entry field is listed in the results pane.
i'm missing something from the re i reckon. any ideas?

G. Willoughby

"Mirko Liss" <mirko.liss at web.de> wrote in message
news:20010729003831.2A193BED.NOFFLE at niccolo.ke4.de...
>
> On Sat, 28 Jul 2001, G. Willoughby wrote:
> > What RE would i use to match this whole word, 'Rasping'
> >
> > i was using:
> >
> > searchString = re.compile('Rasping', re.IGNORECASE) but this don't seem
to
> > be right. Im missing something! any help would be appreiciated!
>
> Hmm, what about some examples:
>
> Python 2.1.1 (#4, Jul 25 2001, 10:17:07)
> [GCC 2.95.2 19991024 (release)] on linux2
> Type "copyright", "credits" or "license" for more information.
> >>> import re
> >>> pattern = re.compile('RASping', re.IGNORECASE)
> >>> pattern
> <SRE_Pattern object at 0x81ed3c8>
> >>> pattern.match("rasping racing pong")
> <SRE_Match object at 0x81f1628>
> >>> pattern.match("rasping racing pong").group(0)
> 'rasping'
> >>> pattern.match("Grasping racing pong").group(0)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> AttributeError: 'None' object has no attribute 'group'
> >>> print pattern.match("Grasping racing pong")
> None
> >>> pattern.search("Grasping racing pong").group(0)
> 'rasping'
> >>> pattern.search("Grasping racing pong").span(0)
> (1, 8)
>
> I believe the Python Documentation and the FAQ explains
> this in more detail. Python reg exps are quite similar
> to Perl reg exps, so there's a huge pile of books at
> the friendly bookshop in your neighborhood, as well.
>
>
> regards,
>
> Mirko
>







More information about the Python-list mailing list