我的文件中有以下行:
normal line
"line in quotes"
line with "quoted" word
line with quotes word at "end"
"quoted line with quoted word at "end""
我只需删除外部引号。
结果:
normal line
line in quotes
line with "quoted" word
line with quotes word at "end"
quoted line with quoted word at "end"
可以使用一个preg_replace()
来完成吗?
答案 0 :(得分:1)
这样的事情:
$result = preg_replace('~^"((?:[^"\r\n]+|"[^\r\n"]*")*+)"$~m', '$1', $text);
模式细节:
~ # pattern delimiter
^ # anchor for the begining of the line
" #
( # open the capture group 1
(?: # non-capturing group, content of a line between quotes:
[^"\r\n]+ # - all that is not a quote or a newline
| # OR
"[^\r\n"]*" # - a substring between quotes
)*+ # repeat the group zero or more times
) # close the capture group
" #
$ # anchor for the end of the line
~m # multiline modifier to change ^ and $
答案 1 :(得分:0)
Preg_replace没有" if"有点可以说"如果在开头和结尾都有引号。"所以,你必须表达两种可能性:
$r = preg_replace('/^"(.*)"$|^(.*)$/', '$1$2', $b);
|的左边匹配一行"在开始和结束。 $ 1之间的文字。如果它不匹配,则$ 1为空。 |的右侧匹配其他任何东西,其中2美元就在那里。如果左侧匹配,它就不会向右侧移动。