Reputation: 79
I am new to Regex and AppleScript and I need a little bit of support and guidence.
First user inputs a string. It could be anything in one or multilines.
A Regex should be applied on the string in order to find numbers with only 6 digits..no more or less, and separates them by a space.
The final string should look like: 867689, 867617, 866478, 866403, 866343
.
Then this string will be converted into a list. I am using this site to test my Regexes : https://www.freeformatter.com/regex-tester.html
The Regex that matches exactly 6 digits is:
(?<!\d)\d{6}(?!\d)
I am aware that in order to implement Regex to AppleScript i need to use Shell Script. I also am aware that I should use sed
but unfortunately I am not fully aware how to use it and what exactly is.
Fallowing a few guides and tests I understood that sed does not work with \d
and I should use [0-9]
instead and I also should escape the brackets like this \(..\)
. Also replace $1,
should be implemented like \1,
. Till this moment I was not able to make it work.
My user input for tests is as follows:
MASTER
ARTIKEL
Artikel
5910020015
867689
PULL1/1
5910020022
867617
PULL1/1
Cappuccino
5910020017
866478
PULL1/1
Braun
5921020017
866403
SHIRT1/2
Kastanie-Multi
5910020016
866343
PULL1/1
and the AppleScript Code itself:
use scripting additions
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
on list2string(theFoldersList, theDelimiter)
set theBackup to AppleScript's text item delimiters
set AppleScript's text item delimiters to theDelimiter
set theString to theFoldersList as string
set AppleScript's text item delimiters to theBackup
return theString
end list2string
on run {input}
display dialog "Please enter your string: " default answer ""
set stringOfNumbers to the text returned of the result
set num to do shell script "sed 's/\(\(?<![0-9]\)[0-9]{6}\(?![0-9]\)\)\1, /' <<< " & quoted form of stringOfNumbers
--(?<!\d)\d{6}(?!\d)
display dialog stringOfNumbers
set stringOfNumbers to current application's NSString's stringWithString:stringOfNumbers
set listOfArtNumbers to (stringOfNumbers's componentsSeparatedByString:", ") as list
display dialog list2string(listOfArtNumbers, ", ")
return input
end run
Unfortunately everywhere I escape characters by using \
I get an error. So I had to remove all \
but once I run the script I receive "Syntax Error: sed: 1: "s/(?<![0-9])[0-9]{6}(?! ...": unterminated substitute pattern"
and all my effort resulted in a similar error.
Upvotes: 2
Views: 1367
Reputation: 3194
AppleScript Objective-C allows us to do regular expressions using NSRegularExpression
, starting with OS 10.7 (Lion). The following handler returns the results of a regular expressions search as a list:
use AppleScript version "2.4"
use framework "Foundation"
property NSRegularExpression : class "NSRegularExpression"
property NSString : class "NSString"
on findPattern:thePattern inString:theString
set theText to NSString's stringWithString:theString
set theRegEx to NSRegularExpression's regularExpressionWithPattern:thePattern ¬
options:0 |error|:(missing value)
set theResult to (theRegEx's matchesInString:theText ¬
options:0 ¬
range:{location:0, |length|:theText's |length|})'s valueForKey:("range")
set outputArray to {}
repeat with thisRange in theResult
copy (theText's substringWithRange:thisRange) as text to end of outputArray
end repeat
return outputArray
end findPattern:inString:
Note that the '¬' symbol is a line-continuation symbol (type option-return in the AppleScript editor). I've broken up lines to make the script more readable, but that may not copy/paste correctly, so be aware that those should be single, continuous lines.
You use this handler as follows. Remember that the backslash is a special character in AppleScript, so it has to be escaped by preceding it with another backslash:
set foundList to my findPattern:"(?<!\\d)\\d{6}(?!\\d)" inString:"MASTER
ARTIKEL
Artikel
5910020015
867689
PULL1/1
5910020022
867617
PULL1/1
Cappuccino
5910020017
866478
PULL1/1
Braun
5921020017
866403
SHIRT1/2
Kastanie-Multi
5910020016
866343
PULL1/1"
-- Result: {"867689", "867617", "866478", "866403", "866343"}
EDIT
It seems Automator doesn't like the property ClassName : class "ClassName"
method I've used, so we have to switch to another form: using current application's ClassName's ...
The revised Automator AppleScript looks like so (assuming that the text string is passed in as the input):
use AppleScript version "2.4"
use framework "Foundation"
on run {input, parameters}
set foundList to my findPattern:"(?<!\\d)\\d{6}(?!\\d)" inString:((item 1 of input) as text)
return foundList
end run
on findPattern:thePattern inString:theString
set theText to current application's NSString's stringWithString:theString
set theRegEx to current application's NSRegularExpression's regularExpressionWithPattern:thePattern ¬
options:0 |error|:(missing value)
set theResult to (theRegEx's matchesInString:theText ¬
options:0 ¬
range:{location:0, |length|:theText's |length|})'s valueForKey:("range")
set outputArray to {}
repeat with thisRange in theResult
copy (theText's substringWithRange:thisRange) as text to end of outputArray
end repeat
return outputArray
end findPattern:inString:
Upvotes: 3