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.
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)
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
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
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.