除非引用,否则‘字符串为单词’是什么操作?

2026-04-01 20:051阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计478个文字,预计阅读时间需要2分钟。

除非引用,否则‘字符串为单词’是什么操作?

为了实现您的要求,您需要将字符串分割成单词,并且去掉单词周围的引号。以下是修改后的代码:

pythontext=I 'am 'the text'

使用split方法分割字符串,它会自动处理引号内的内容words=text.split()

去掉每个单词两端的引号formatted_words=[word.strip(') for word in words]

输出结果print( .join(formatted_words))

结果将会是:I am the text

所以我有以下代码在空格之间拆分字符串:

text = "I am 'the text'" for string in text:gmatch("%S+") do print(string) end

结果:

除非引用,否则‘字符串为单词’是什么操作?

I am 'the text'

但我需要这样做:

I am the text --[[yep, without the quotes]]

我怎样才能做到这一点?

编辑:只是为了补充问题,我们的想法是将参数从程序传递到另一个程序.这是我正在工作的拉取请求,目前正在审核中:github.com/mpv-player/mpv/pull/1619

有可能通过巧妙的解析来实现这一点,但另一种方法可能是跟踪简单状态并根据引用片段的检测合并片段.这样的事情可能有用:

local text = [[I "am" 'the text' and "some more text with '" and "escaped \" text"]] local spat, epat, buf, quoted = [=[^(['"])]=], [=[(['"])$]=] for str in text:gmatch("%S+") do local squoted = str:match(spat) local equoted = str:match(epat) local escaped = str:match([=[(\*)['"]$]=]) if squoted and not quoted and not equoted then buf, quoted = str, squoted elseif buf and equoted == quoted and #escaped % 2 == 0 then str, buf, quoted = buf .. ' ' .. str, nil, nil elseif buf then buf = buf .. ' ' .. str end if not buf then print((str:gsub(spat,""):gsub(epat,""))) end end if buf then print("Missing matching quote for "..buf) end

这将打印:

I am the text and some more text with ' and escaped \" text

更新以处理混合和转义引号.已更新以删除引号.更新以处理引用的单词.

本文共计478个文字,预计阅读时间需要2分钟。

除非引用,否则‘字符串为单词’是什么操作?

为了实现您的要求,您需要将字符串分割成单词,并且去掉单词周围的引号。以下是修改后的代码:

pythontext=I 'am 'the text'

使用split方法分割字符串,它会自动处理引号内的内容words=text.split()

去掉每个单词两端的引号formatted_words=[word.strip(') for word in words]

输出结果print( .join(formatted_words))

结果将会是:I am the text

所以我有以下代码在空格之间拆分字符串:

text = "I am 'the text'" for string in text:gmatch("%S+") do print(string) end

结果:

除非引用,否则‘字符串为单词’是什么操作?

I am 'the text'

但我需要这样做:

I am the text --[[yep, without the quotes]]

我怎样才能做到这一点?

编辑:只是为了补充问题,我们的想法是将参数从程序传递到另一个程序.这是我正在工作的拉取请求,目前正在审核中:github.com/mpv-player/mpv/pull/1619

有可能通过巧妙的解析来实现这一点,但另一种方法可能是跟踪简单状态并根据引用片段的检测合并片段.这样的事情可能有用:

local text = [[I "am" 'the text' and "some more text with '" and "escaped \" text"]] local spat, epat, buf, quoted = [=[^(['"])]=], [=[(['"])$]=] for str in text:gmatch("%S+") do local squoted = str:match(spat) local equoted = str:match(epat) local escaped = str:match([=[(\*)['"]$]=]) if squoted and not quoted and not equoted then buf, quoted = str, squoted elseif buf and equoted == quoted and #escaped % 2 == 0 then str, buf, quoted = buf .. ' ' .. str, nil, nil elseif buf then buf = buf .. ' ' .. str end if not buf then print((str:gsub(spat,""):gsub(epat,""))) end end if buf then print("Missing matching quote for "..buf) end

这将打印:

I am the text and some more text with ' and escaped \" text

更新以处理混合和转义引号.已更新以删除引号.更新以处理引用的单词.