Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 删除AppleScript中的匹配文本_Regex_Variables_Applescript - Fatal编程技术网

Regex 删除AppleScript中的匹配文本

Regex 删除AppleScript中的匹配文本,regex,variables,applescript,Regex,Variables,Applescript,使用AppleScript,我正在运行一个shell脚本: set username to (do shell script "whoami") 它将以预先指定公司名称的方式返回用户名。有没有办法找到匹配的文本,并将其从username变量中删除 例如,如果用户名是businessName\username 如何删除businessName \请尝试: set myText to "businessName\\username" set {TID, text item delimiters

使用AppleScript,我正在运行一个shell脚本:

set username to (do shell script "whoami") 
它将以预先指定公司名称的方式返回用户名。有没有办法找到匹配的文本,并将其从username变量中删除

例如,如果用户名是businessName\username

如何删除businessName \

请尝试:

set myText to "businessName\\username"

set {TID, text item delimiters} to {text item delimiters, "\\"}
set myText to text item 2 of myText
set text item delimiters to TID

return myText

为什么不直接检索$USER

set currentuser to (do shell script "echo $USER")
另一种方法是

set currentuser to system attribute "USER"
(注意使用大写字符)。

使用sed的解决方案:

set username to do shell script "whoami|sed 's/^.*\\\\//'"

(反斜杠在bash中必须是带反斜杠的掩码,在applescript中两者都必须是带反斜杠的掩码。)

这很好,除非只有用户名有2\\n时才有效。在此之前,我们只有一个。这就是转义\字符的方法。用你的剧本试试看。啊哈,太好了!谢谢你!如果这真的是关于获取用户名,那么您最好将标题设置为“获取用户名”。无需在标题中提及AppleScript,因为它是一个标记名。但它不是。我有用户名。我想从用户名中删除某些文本。看起来adayzdone给出了正确的答案。我现在知道您不是在查找用户名,而是要更改$user返回的值。