Hallo,
ich habe hier mal ne Suchen/Ersetzen-Funktion für Applescript "entwickelt" und diese kann sicher optimiert werden.
Frage??
- wie kann man die als OSAX schreiben.
- kann man die Funktion noch optimieren?
Alles anzeigen
In s und e kann man alle möglichen Regex reinsschreiben, ich konnte keinen Fehler mehr feststellen, wenn man den Backslash beachtet.
Gruß fiveyears
ich habe hier mal ne Suchen/Ersetzen-Funktion für Applescript "entwickelt" und diese kann sicher optimiert werden.
Frage??
- wie kann man die als OSAX schreiben.
- kann man die Funktion noch optimieren?
Quellcode
- on regex(s, e, aValue)
- -- aValue: String oder Liste
- -- s zu suchen
- -- e zu ersetzen
- -- \ ist in s oder e als \\\\ anzugeben, in aValue reicht \\
- if s is equal to "" then
- return aValue
- end if
- set oldTID to AppleScript's text item delimiters
- set retL to {}
- if class of aValue as string is equal to "list" then
- set noList to false
- set myList to aValue
- else
- set noList to true
- set myList to {}
- set the end of myList to aValue
- end if
- if s contains "/" then
- set AppleScript's text item delimiters to "/"
- set theList to (every text item of s)
- set AppleScript's text item delimiters to "\\/"
- set s to theList as string
- end if
- if e contains "/" then
- set AppleScript's text item delimiters to "/"
- set theList to (every text item of e)
- set AppleScript's text item delimiters to "\\/"
- set e to theList as string
- end if
- repeat with p in myList
- if p is equal to "" then
- ret = ""
- else
- if p contains "\"" then
- set AppleScript's text item delimiters to "\""
- set theList to (every text item of p)
- set AppleScript's text item delimiters to "\\\""
- set p to theList as string
- end if
- if p contains "\\" then
- set AppleScript's text item delimiters to "\\"
- set theList to (every text item of p)
- set AppleScript's text item delimiters to "\\\\"
- set p to theList as string
- end if
- -- set ret to do shell script " echo " & quoted form of p & " | " & ¬
- set perlScript to "$_=\"" & p & "\";s/" & s & "/" & e & "/g;print;"
- set ret to do shell script "perl -e " & quoted form of perlScript
- end if
- set the end of retL to ret
- end repeat
- set AppleScript's text item delimiters to oldTID
- if noList then
- return item 1 of retL
- else
- return retL
- end if
- return ret
- end regex
In s und e kann man alle möglichen Regex reinsschreiben, ich konnte keinen Fehler mehr feststellen, wenn man den Backslash beachtet.
Gruß fiveyears