Home > XModules >

Requirements

  • OS X 10.9 or later
    • Developed and tested on macOS 10.14.6

Download

Version 1.0.1 -- 2021-04-09

Dependencies

XRegex

XRegex is an AppleScript library to provide pattern matthing with regular expressions.

You can obtain matched texts and replace these.

NSRegularExpression is used in the interanl.

use XRegex : script "XRegex"

(*** Using Class methods ***)
-- Obtain first matche
tell XRegex's search("\\d{4}", "1972-01-27")
if it is not missing value then
log capture(0) (*1972*)
log range(0) (*length:4, location:0*)
end if
end tell

-- Obtain all mathes
tell XRegex's search_all("\\d{2}", "1972-01-27")
repeat with a_match in it
tell a_match
log capture(0)
log range(0)
(*19*)
(*length:2, location:0*)
(*72*)
(*length:2, location:2*)
(*01*)
(*length:2, location:5*)
(*27*)
(*length:2, location:8*)
end tell
end repeat
end tell

-- Replace
log XRegex's replace_matches("\\d{2}", "1972-01-27", "xx") (*xxxx-xx-xx*)

(*** Compiling Regular Expression and Working with Instance Methods ***)
-- Simple cases
tell XRegex's make_with("(\\d{4})-\\d{1,2}-\\d{1,2}")
log count_in("2020-10-10") (*1*)

tell first_match("2020-10-10")
if it is not missing value then
log (count it) (*2*)
log its capture(0) -- 2020-10-10 : whole match
log its capture(1) -- 2020 : first capture
end if
end tell
end tell

-- Obtain matched all sub-texts
tell XRegex's make_with("\\d{2}")
set matches to matches_in("2021-10-20")
end tell
log (count matches) (*4*)
tell item 1 of matches
log (count it) (*1*)
log capture(0) (*20*)
log range(0) (*length:2, location:0*)
end tell

tell item 2 of matches
log (count it) (*1*)
log capture(0) (*21*)
set {location:loc, length:len} to range(0) (*length:2, location:2*)
--obtain substring
log substring(loc + 3, len) (*10*)
end tell


-- Replace
tell XRegex's make_with("http://([^/]+)(/[^/]+)*")
log replace("http://www.script-factory.net/index.html", ¬
"https://$1")
(*https://www.script-factory.net*)
end tell

-- split
tell XRegex's make_with("\\s*,\\s*")
log split("a , b,c")
(*a, b, c*)
end tell

return true

History

  • 1.0.1 -- 2021-04-09
    • Added “split”
  • 1.0 -- 2020-06-26
    • First Release