在两个字符串 AppleScript 方法之间提取文本的错误
Bugs with extracting text between two strings AppleScript method
这里是 AppleScript 的新手,我从其他在线资源中学到了以下方法,它允许我提取我可以定义的两个字符串之间的字符串,请参见代码:
to ExText(searchText, startText, endText)
set spaceholder to "x"
set searchText to ("x" & searchText)
set AppleScript's text item delimiters to spaceholder
set endItems to text item -1 of searchText
set AppleScript's text item delimiters to endText
set beginningToEnd to text item 1 of endItems
set AppleScript's text item delimiters to startText
set finalText to (text items 2 thru -1 of beginningToEnd) as text
set AppleScript's text item delimiters to ""
return finalText
end ExText
我正在使用此方法从通过 运行 Javascript id 搜索获得的 html 字符串中提取用户的名字和姓氏。我会做
Set SourceString to "<span>firstname lastname</span>"
ExText(SourceString, "<span>", "</span>")
它在 90% 的时间都有效,但在少数情况下,根据用户的姓名,它会收到一条错误消息
Can’t make text items 2 thru -1 of "(part of the name)" into type text.
下面是一些会破坏此方法的示例名称
<span>Xu Chang</span>
<span>Maxim Smith</span>
在进一步测试中,我确认任何包含字母 "X" 的名称都无法通过此方法。我已经检查了实际的源字符串与包含 X 的名称和不包含 X 的名称,所以它不是源字符串而是方法本身。
值得注意的是,这个错误每次都会重现。
一个相当奇怪的行为。有什么想法吗?
将 spaceholder
的变量从 "x" 更改为 "xx" 应该可行
to ExText(searchText, startText, endText)
set spaceholder to "xx"
set searchText to ("xx" & searchText)
set AppleScript's text item delimiters to spaceholder
set endItems to text item -1 of searchText
set AppleScript's text item delimiters to endText
set beginningToEnd to text item 1 of endItems
set AppleScript's text item delimiters to startText
set finalText to (text items 2 thru -1 of beginningToEnd) as text
set AppleScript's text item delimiters to ""
return finalText
end ExText
这里是 AppleScript 的新手,我从其他在线资源中学到了以下方法,它允许我提取我可以定义的两个字符串之间的字符串,请参见代码:
to ExText(searchText, startText, endText)
set spaceholder to "x"
set searchText to ("x" & searchText)
set AppleScript's text item delimiters to spaceholder
set endItems to text item -1 of searchText
set AppleScript's text item delimiters to endText
set beginningToEnd to text item 1 of endItems
set AppleScript's text item delimiters to startText
set finalText to (text items 2 thru -1 of beginningToEnd) as text
set AppleScript's text item delimiters to ""
return finalText
end ExText
我正在使用此方法从通过 运行 Javascript id 搜索获得的 html 字符串中提取用户的名字和姓氏。我会做
Set SourceString to "<span>firstname lastname</span>"
ExText(SourceString, "<span>", "</span>")
它在 90% 的时间都有效,但在少数情况下,根据用户的姓名,它会收到一条错误消息
Can’t make text items 2 thru -1 of "(part of the name)" into type text.
下面是一些会破坏此方法的示例名称
<span>Xu Chang</span>
<span>Maxim Smith</span>
在进一步测试中,我确认任何包含字母 "X" 的名称都无法通过此方法。我已经检查了实际的源字符串与包含 X 的名称和不包含 X 的名称,所以它不是源字符串而是方法本身。
值得注意的是,这个错误每次都会重现。
一个相当奇怪的行为。有什么想法吗?
将 spaceholder
的变量从 "x" 更改为 "xx" 应该可行
to ExText(searchText, startText, endText)
set spaceholder to "xx"
set searchText to ("xx" & searchText)
set AppleScript's text item delimiters to spaceholder
set endItems to text item -1 of searchText
set AppleScript's text item delimiters to endText
set beginningToEnd to text item 1 of endItems
set AppleScript's text item delimiters to startText
set finalText to (text items 2 thru -1 of beginningToEnd) as text
set AppleScript's text item delimiters to ""
return finalText
end ExText