• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSPredicate regex
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSPredicate regex


  • Subject: Re: NSPredicate regex
  • From: "Stephen J. Butler" <email@hidden>
  • Date: Fri, 19 Feb 2010 01:17:25 -0600

On Tue, Feb 16, 2010 at 9:55 AM, martin halter <email@hidden> wrote:
>    NSString *regex = @"(?:.*[\\s\\W0-9])*cache(?:[\\s\\W0-9].*)*";

Your regex is pathological. It's not hard to confuse the matcher with
really convoluted regular expressions. Consider this: how is your
pattern any different from...

NSString *regex = @"cache";

Since your atoms before and after "cache" can match 0 or more times,
you could greatly simplify the thing by just not matching at all!
Never mind the .* inside the atoms.

If you want to match the word cache that is surrounded by some sort of
defined separators, I'd suggest:

(^|[\s\W0-9])cache([\s\W0-9]|$)

Broken down:

- the start of the string, or some other selected separator
- cache
- some other selected separator, or the end of the string

That should match cache in this instances:

"cache"
"cache some other stuff"
"some other stuff cache"
"some other cache stuff"
"cache/some other stuff"
"some other stuff/cache"
"some other/cache/stuff"
"cache0some other stuff"
"some other stuff0cache"
"some other1cache2stuff"

But not: "some othercachestuff"

Note: I'm not familiar with the particular flavor of regexes ICU uses.
More used to Java and PCRE. But this is the basic idea.
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Re: NSPredicate regex (From: martin halter <email@hidden>)

  • Prev by Date: Re: How to detect if mouse on border of view
  • Next by Date: NSForegroundColorAttributeName without AppKit
  • Previous by thread: Re: NSPredicate regex
  • Next by thread: Help with creating CustomViews
  • Index(es):
    • Date
    • Thread