Sed Applescript中高效的字符串编辑

Sed Applescript中高效的字符串编辑,sed,terminal,applescript,Sed,Terminal,Applescript,我正在写一个applescript,它需要获取一个字符串,然后只输出该字符串中的数字。我有一个有效的方法 do shell script“sed s/[a-zA-Z\\\\\']//g您是否尝试过tr tr -d '[:alpha:][:punct:]' <<< string tr-d'[:alpha:][:punct:][p>你试过了吗 tr -d '[:alpha:][:punct:]' <<< string tr-d'[:alpha:][:pun

我正在写一个applescript,它需要获取一个字符串,然后只输出该字符串中的数字。我有一个有效的方法


do shell script“sed s/[a-zA-Z\\\\\']//g您是否尝试过
tr

 tr -d '[:alpha:][:punct:]' <<< string

tr-d'[:alpha:][:punct:][p>你试过了吗

 tr -d '[:alpha:][:punct:]' <<< string

tr-d'[:alpha:][:punct:][p>你试过了吗

 tr -d '[:alpha:][:punct:]' <<< string

tr-d'[:alpha:][:punct:][p>你试过了吗

 tr -d '[:alpha:][:punct:]' <<< string

tr-d'[:alpha:::[:punct:]”如果您真的必须一次从字符串中选择数字,那么这种方法可能会更快,尽管它看起来要快得多,但我应该告诉您,指定tr命令的完整路径将节省一些时间,如果只是几毫秒的话

set mlist to "1 man paid 1,23 cents for 51 can\\'s of beer "
 script o
    property l : missing value
 end script
 set o's l to words of mlist
 repeat with i from 1 to (count o's l)
    try
        item i of o's l as integer
    on error
        set item i of o's l to missing value
    end try
 end repeat
 set o's l to o's l's text

 set tids to my text item delimiters
 set my text item delimiters to space
 set o's l to text items of o's l
 set my text item delimiters to tids
 set o's l to o's l as text
 set my text item delimiters to ","
 set o's l to text items of o's l
 set my text item delimiters to tids
 set numb3rs to o's l as text

 log numb3rs
 --> (*112351*)

这种方法可能更快,因为您可以为获得的每一行节省do shell脚本的开销,如果这些数字格式正确(正确的十进制分隔符),它还应该只返回数字。我没有尝试过使用e-notation,但我认为这也应该有效。

如果您确实必须一次从字符串中选择数字,那么这种方法可能会更快,尽管它看起来要快得多,但我应该告诉您,指定tr命令的完整路径将节省一些,如果只是几毫秒的话ds

set mlist to "1 man paid 1,23 cents for 51 can\\'s of beer "
 script o
    property l : missing value
 end script
 set o's l to words of mlist
 repeat with i from 1 to (count o's l)
    try
        item i of o's l as integer
    on error
        set item i of o's l to missing value
    end try
 end repeat
 set o's l to o's l's text

 set tids to my text item delimiters
 set my text item delimiters to space
 set o's l to text items of o's l
 set my text item delimiters to tids
 set o's l to o's l as text
 set my text item delimiters to ","
 set o's l to text items of o's l
 set my text item delimiters to tids
 set numb3rs to o's l as text

 log numb3rs
 --> (*112351*)

这种方法可能更快,因为您可以为获得的每一行节省do shell脚本的开销,如果这些数字格式正确(正确的十进制分隔符),它还应该只返回数字。我没有尝试过使用e-notation,但我认为这也应该有效。

如果您确实必须一次从字符串中选择数字,那么这种方法可能会更快,尽管它看起来要快得多,但我应该告诉您,指定tr命令的完整路径将节省一些,如果只是几毫秒的话ds

set mlist to "1 man paid 1,23 cents for 51 can\\'s of beer "
 script o
    property l : missing value
 end script
 set o's l to words of mlist
 repeat with i from 1 to (count o's l)
    try
        item i of o's l as integer
    on error
        set item i of o's l to missing value
    end try
 end repeat
 set o's l to o's l's text

 set tids to my text item delimiters
 set my text item delimiters to space
 set o's l to text items of o's l
 set my text item delimiters to tids
 set o's l to o's l as text
 set my text item delimiters to ","
 set o's l to text items of o's l
 set my text item delimiters to tids
 set numb3rs to o's l as text

 log numb3rs
 --> (*112351*)

这种方法可能更快,因为您可以为获得的每一行节省do shell脚本的开销,如果这些数字格式正确(正确的十进制分隔符),它还应该只返回数字。我没有尝试过使用e-notation,但我认为这也应该有效。

如果您确实必须一次从字符串中选择数字,那么这种方法可能会更快,尽管它看起来要快得多,但我应该告诉您,指定tr命令的完整路径将节省一些,如果只是几毫秒的话ds

set mlist to "1 man paid 1,23 cents for 51 can\\'s of beer "
 script o
    property l : missing value
 end script
 set o's l to words of mlist
 repeat with i from 1 to (count o's l)
    try
        item i of o's l as integer
    on error
        set item i of o's l to missing value
    end try
 end repeat
 set o's l to o's l's text

 set tids to my text item delimiters
 set my text item delimiters to space
 set o's l to text items of o's l
 set my text item delimiters to tids
 set o's l to o's l as text
 set my text item delimiters to ","
 set o's l to text items of o's l
 set my text item delimiters to tids
 set numb3rs to o's l as text

 log numb3rs
 --> (*112351*)

这种方法可能更快,因为您可以为获得的每一行节省do shell脚本的开销,如果这些数字格式正确(正确的十进制分隔符),它还应该只返回数字。我还没有尝试过使用e-notation,但我认为这也应该有效。

另一个sed解决方案,以确保只显示数字:

do shell script "echo " & quoted form of s & "|sed s/[^0-9]//g"

另一个sed解决方案,确保只显示数字:

do shell script "echo " & quoted form of s & "|sed s/[^0-9]//g"

另一个sed解决方案,确保只显示数字:

do shell script "echo " & quoted form of s & "|sed s/[^0-9]//g"

另一个sed解决方案,确保只显示数字:

do shell script "echo " & quoted form of s & "|sed s/[^0-9]//g"

我提供了这个替代代码。我不明白为什么McUsr的代码需要这么复杂

set s to "1 man paid 1,23 cents for 51 can\\'s of beer "
set n to ""

repeat with i from 1 to (length of s)
    set c to (character i of s)

    # http://stackoverflow.com/questions/20313799
    if class of c is number then
        set n to n & c
    end if

end repeat

return n

我提供了这个替代代码。我不明白为什么McUsr的代码需要这么复杂

set s to "1 man paid 1,23 cents for 51 can\\'s of beer "
set n to ""

repeat with i from 1 to (length of s)
    set c to (character i of s)

    # http://stackoverflow.com/questions/20313799
    if class of c is number then
        set n to n & c
    end if

end repeat

return n

我提供了这个替代代码。我不明白为什么McUsr的代码需要这么复杂

set s to "1 man paid 1,23 cents for 51 can\\'s of beer "
set n to ""

repeat with i from 1 to (length of s)
    set c to (character i of s)

    # http://stackoverflow.com/questions/20313799
    if class of c is number then
        set n to n & c
    end if

end repeat

return n

我提供了这个替代代码。我不明白为什么McUsr的代码需要这么复杂

set s to "1 man paid 1,23 cents for 51 can\\'s of beer "
set n to ""

repeat with i from 1 to (length of s)
    set c to (character i of s)

    # http://stackoverflow.com/questions/20313799
    if class of c is number then
        set n to n & c
    end if

end repeat

return n


这无疑是一个进步,它似乎缩短了3-4分钟的运行时间。很高兴知道这一点……如果有人问这个问题,我会记住这一点……D……更好的是,使用“补码”标志:tr-D-c'[0-9]'这绝对是一个进步,它似乎缩短了3-4分钟的运行时间。很高兴知道这一点…如果有人问这个问题,我会记住这一点。:D..更好的是,使用“补码”标志:tr-D-c'[0-9]'这绝对是一个进步,它似乎缩短了3-4分钟的运行时间。很高兴知道这一点…如果有人问这个问题,我会记住这一点。:D..更好的是,使用“补码”标志:tr-D-c'[0-9]'这绝对是一个进步,它似乎缩短了3-4分钟的运行时间。很高兴知道这一点…如果有人问这个问题,我会记住这一点。:D..更好的是,使用“补码”标志:tr-D-c'[0-9]“如果你能传递整个文本,而不是一次传递一个字符串,那么你可以节省很多通话时间。只要提及它。你是否使用10.10+?如果是,请通过AppleScript ObjC使用
NSRegularExpression
。或者使用第三方Satimage.osax的
find text
命令,该命令也适用于早期的操作系统版本。你能提供输入和预期的结果吗?”输出?我需要将字符串作为单独的实体保留。是否有方法进行组合、筛选,然后再次分离?如果可以传递整个文本,而不是一次只传递一个字符串,您将节省大量调用。仅提及它。您是否使用10.10+?如果是,请通过AppleScript ObjC使用
NSRegularExpression
。或者使用第三方Satimage.osax's的
find text
命令,该命令也适用于早期的操作系统版本。你能提供输入和预期输出吗?我需要将字符串作为单独的实体保留。有没有办法组合、筛选,然后再次分离?如果你能一次传递整个文本,而不仅仅是一个字符串,你会节省很多调用。只是提一下。你呢在10.10+上?如果是这样,请通过AppleScript ObjC使用
NSRegularExpression
。或者使用第三方Satimage.osax的
查找文本
命令,该命令也适用于早期操作系统版本。您能提供输入和预期输出吗?我需要将字符串作为单独的实体保留。是否有方法进行组合、筛选,然后再次分离?您需要如果你可以传递整个文本,而不是一次只传递一个字符串,那么我会打很多电话。只需提及它。你是否使用10.10+?如果是,请通过AppleScript ObjC使用
NSRegularExpression
。或者使用第三方Satimage.osax的
fin