给定一个字符串,如果它们存在,如何删除周围的引号?
Given a string how to remove surrounding quotes if they exist?
给定以下字符串:
- 你好世界
- 你好"world"
- 你好"world"你好
- "Hello World"
- "Hello World. How's life?"
删除以字符串开头和结尾的可能字符串的最佳方法是什么。 func 所需的 return 将是:
- 你好世界
- 你好"world"
- 你好"world"你好
- 你好世界
- 你好世界。生活怎么样?
如果您谈论的是删除字符串开头和结尾的引号仅:
string.sub(/\A"/, '').sub(/"\z/, '')
如果你的意思是只有 如果出现在开始和结束处:
string.sub(/\A"(.*)"\z/, '\1')
使用编号保存(有时称为 "registers")和 sed 非常简单:
$ cat quotes
Hello World
Hello "world"
Hello "world" hello
"Hello World"
"Hello World. How's life?"
$ cat quotes | sed 's/^\(\"\)\(.*\)$//'
Hello World
Hello "world"
Hello "world" hello
Hello World
Hello World. How's life?
sed 脚本说:"at the beginning of the line ^, if there is a one double quotation mark " 将它存储在 \("\) 中编号保存一,存储其他任何内容 \(.*\),直到编号保存一在行 $ , in numbered save two, 然后把整个东西替换成numbered save two (双引号之间的其他任何东西). 如果开头没有引号, numbered save one是空的, 如果开头有但 none 最后,则模式不匹配。
还有其他脚本语言,例如 perl,可以轻松处理正则表达式。
给定以下字符串:
- 你好世界
- 你好"world"
- 你好"world"你好
- "Hello World"
- "Hello World. How's life?"
删除以字符串开头和结尾的可能字符串的最佳方法是什么。 func 所需的 return 将是:
- 你好世界
- 你好"world"
- 你好"world"你好
- 你好世界
- 你好世界。生活怎么样?
如果您谈论的是删除字符串开头和结尾的引号仅:
string.sub(/\A"/, '').sub(/"\z/, '')
如果你的意思是只有 如果出现在开始和结束处:
string.sub(/\A"(.*)"\z/, '\1')
使用编号保存(有时称为 "registers")和 sed 非常简单:
$ cat quotes
Hello World
Hello "world"
Hello "world" hello
"Hello World"
"Hello World. How's life?"
$ cat quotes | sed 's/^\(\"\)\(.*\)$//'
Hello World
Hello "world"
Hello "world" hello
Hello World
Hello World. How's life?
sed 脚本说:"at the beginning of the line ^, if there is a one double quotation mark " 将它存储在 \("\) 中编号保存一,存储其他任何内容 \(.*\),直到编号保存一在行 $ , in numbered save two, 然后把整个东西替换成numbered save two (双引号之间的其他任何东西). 如果开头没有引号, numbered save one是空的, 如果开头有但 none 最后,则模式不匹配。 还有其他脚本语言,例如 perl,可以轻松处理正则表达式。