ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> pythoncard
pythoncard
[Pythoncard-users] findString function added to util.py
by Kevin Altis other posts by this author
Jul 8 2002 8:01PM messages near this date
[Pythoncard-users] FW: [Python-Dev] New Persistence SIG created | [Pythoncard-users] regular expressions in Python
As mentioned previously, \b seems to work for matching word boundaries. I've
updated the companies sample to support whole word searching in the Find
dialog. This is the function I came up with to do the work:

def findString(pattern, text, caseSensitive=0, wholeWordsOnly=0):
    """
    search for pattern in text and return the offset
    of the first match or -1 if the text is not found
    """
    if wholeWordsOnly or not caseSensitive:
        # we could probably just always use
        # a regular expression, but I'm guessing
        # that string find() is quicker
        if wholeWordsOnly:
            pattern = r'\b' + pattern + r'\b'
        if caseSensitive:
            p = re.compile(pattern)
        else:
            p = re.compile(pattern, re.IGNORECASE)
        match = p.search(text)
        if match == None:
            return -1
        else:
            return match.start()
    else:
        return text.find(pattern)

That can probably be made more elegant, suggestions welcome. I stuck it into
util.py, so that any PythonCard app can make use of it. I'll add support for
it in textEditor and other samples that do finds as well.

ka



-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Oh, it's good to be a geek.
http://thinkgeek.com/sf
_______________________________________________
Pythoncard-users mailing list
Pythoncard-users@[...].net
https://lists.sourceforge.net/lists/listinfo/pythoncard-users

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved