Test String against Regex using NSRegularExpression

So, I have a regex of what our password policy is (look at 10.10 / 10.11 password policy setting using pwpolicy for more context). The regex is complex enough that getting true from it is good enough

I’m aware that I could, very easily, use SED and the regex to test a string against

However, as I like a challenge, I’d prefer to do it in ASOC.

I’ve looked at https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSRegularExpression_Class/index.html#//apple_ref/occ/clm/NSRegularExpression/regularExpressionWithPattern:options:error:

and

http://macscripter.net/viewtopic.php?pid=168800#p168800

and

http://macscripter.net/viewtopic.php?pid=173049

It looks like I need to create the pattern, then use the pattern against a given string

Ideas? I gave up after an hour with using sed as we have quite a complex expression

You probably want to look for a match, and see if it matches the whole string. Something like this would work:

set theRegex to current application's NSRegularExpression's regularExpressionWithPattern:"\\d+" options:0 |error|:(missing value)
set anNSString to current application's NSString's stringWithString:searchString
set stringLength to anNSString's |length|()
set matchLength to |length| of (theRegex's rangeOfFirstMatchInString:anNSString options:0 range:{0, stringLength})
return (matchLength = stringLength)

As always, thanks for the reply.

Assuming that NSRegularExpression has Logic operator support, then I should be good, such as:

(?=^(?=^.*(?=.*[0-9]).*$)|(?=^.*(?=.*[A-Z]).*$).*$)

No need to assume; the Xcode docs spell out what’s supported. It’s ICU syntax, so you can also also check it out here: userguide.icu-project.org/strings/regexp

So, after much cursing…

set searchString to "bob$1"
set searchArray to {"^.*(?=.{8,}).*$", "^.*(?=.*\\d).*$", "^.*(?=.*[a-z]).*$", "^.*(?=.*[A-Z]).*$", "^.*(?=.*[@#$%^&+=]).*$"}

set anNSString to current application's NSString's stringWithString:searchString
set stringLength to anNSString's |length|()
repeat with i in searchArray
	set theRegEx to (current application's NSRegularExpression's regularExpressionWithPattern:i options:0 |error|:(missing value))
	set matchLength to |length| of (theRegEx's rangeOfFirstMatchInString:anNSString options:0 range:{0, stringLength})
	log i & " -- result " & matchLength
end repeat

returns:

Is that good news or bad? You’re being a bit cryptic…

Giddy with excitement

works exactly as expected, which is a tremendous thing

Shane wrote:

Open this Scriplet in your Editor:
set theRegex to current application’s NSRegularExpression’s regularExpressionWithPattern:“\d+” options:0 |error|:(missing value)
set anNSString to current application’s NSString’s stringWithString:searchString
set stringLength to anNSString’s |length|()
set matchLength to |length| of (theRegex’s rangeOfFirstMatchInString:anNSString options:0 range:{0, stringLength})
return (matchLength = stringLength)

All I get is:

error “NSRegularExpression doesn’t understand the “regularExpressionWithPattern_options_error_” message.” number -1708 from NSRegularExpression

If you’re running it in other than an Xcode project, you ned to begin the script with:

use framework "Foundation"

It also requires 10.10 or later, or 10.9 and being put in a script library.

Shane,
Is the code that you posted here equivalent to the “findPattern” code on page 70 of your book, 2nd Edition? I’m having my own issues with my own long regex expression and want to make sure I’m using a correct method.

thank you

Would you believe I can’t lay my hands on the second edition right now, but it should be, broadly. Post the code you’re having problems with.