Monday, December 11, 2017

#1 2015-09-22 09:49:12 am

Woggledog
Member
Registered: 2012-12-06
Posts: 150

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 … ions: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

Offline

 

#2 2015-09-22 07:01:41 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5197

Re: Test String against Regex using NSRegularExpression

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

Applescript:

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)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#3 2015-09-23 03:03:29 am

Woggledog
Member
Registered: 2012-12-06
Posts: 150

Re: Test String against Regex using NSRegularExpression

As always, thanks for the reply.

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

Applescript:

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

Offline

 

#4 2015-09-23 03:19:07 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5197

Re: Test String against Regex using NSRegularExpression

Woggledog wrote:

Assuming that NSRegularExpression


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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#5 2015-09-23 05:31:19 am

Woggledog
Member
Registered: 2012-12-06
Posts: 150

Re: Test String against Regex using NSRegularExpression

So, after much cursing.....

Applescript:

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:

(*^.*(?=.{8,}).*$ -- result 0*)
(*^.*(?=.*\d).*$ -- result 5*)
(*^.*(?=.*[a-z]).*$ -- result 5*)
(*^.*(?=.*[A-Z]).*$ -- result 0*)
(*^.*(?=.*[@#$%^&+=]).*$ -- result 5*)

Offline

 

#6 2015-09-23 09:25:35 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5197

Re: Test String against Regex using NSRegularExpression

Is that good news or bad? You're being a bit cryptic...


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#7 2015-09-24 09:45:16 am

Woggledog
Member
Registered: 2012-12-06
Posts: 150

Re: Test String against Regex using NSRegularExpression

Giddy with excitement

works exactly as expected, which is a tremendous thing

Offline

 

#8 2016-01-18 10:01:21 pm

jhaneyzz
Member
Registered: 2010-03-09
Posts: 28

Re: Test String against Regex using NSRegularExpression

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

Offline

 

#9 2016-01-19 12:57:55 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5197

Re: Test String against Regex using NSRegularExpression

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

Applescript:

use framework "Foundation"

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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#10 2016-03-09 06:52:09 pm

btully
Member
Registered: 2009-06-12
Posts: 8

Re: Test String against Regex using 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.

thank you

Offline

 

#11 2016-03-09 07:10:55 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5197

Re: Test String against Regex using NSRegularExpression

btully wrote:

Is the code that you posted here equivalent to the "findPattern" code on page 70 of your book, 2nd Edition?


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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)