这是我的弦。
$text = 'I am [pitch = "high"]Rudratosh Shastri[endpitch]. what is your name ? how are you? sorry [pause ="3000"] i can not hear you ?[rate="-70.00%"] i still can\'t hear you[endrate] ? [rate="+50.00%"]why i can\'t hear you[endrate] ?';
我想用[pause = "3000"]
代替<break time="3000ms">
我写了以下正则表达式,但是它选择的是最后一个"]
\[pause.*\"(\d+)\".*\"]
PHP:$text = preg_replace("/\[pause.*\"(\w+)\".*\"]/", '<break time="$1ms"/>', $text);
如果我要找到一个解决方案,其中正则表达式仅选择“任意数字”
any number"]
我的问题会解决。
但是我找不到解决方法。
您有什么建议吗?
答案 0 :(得分:1)
您可以使用
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 140
}
或者(如果数字后面还有其他内容):
\[pause[^]]*"(\d+)"]
,并替换为\[pause[^]]*"(\d+)"[^]]*]
^^^^^
。参见config.yml
详细信息
<break time="$1ms"/>
-\[pause
子字符串[pause
-除[^]]*
以外的0多个字符]
-双引号"
-第1组:一个或多个数字(\d+)
-一个"]
子字符串。"]
答案 1 :(得分:1)
由于最后一个部分.*\"
,您的正则表达式匹配过多,如果删除了该部分,则可以匹配当前示例数据,但是前一个.*
仍然可以匹配任何字符,包括例如字符像"[]
。
您可以做的是替换第一个.*
,方法是匹配由\h*=\h*
之类的水平空白字符包围的等号
请注意,您不必转义双引号。
您可以使用:
\[pause\h*=\h*"(\d+)"]
这将匹配
\[pause
匹配[pause
\h*
匹配零个或多个水平空白字符=
匹配= \h*"
匹配零个或多个水平空白字符,后跟一个"
(\d+)
分组捕获一个或多个数字"]
匹配"]
并替换为:
<break time="$1ms">
或使用<break time="$1ms"/>
例如:
$text = 'I am [pitch = "high"]Rudratosh Shastri[endpitch]. what is your name ? how are you? sorry [pause ="3000"] i can not hear you ?[rate="-70.00%"] i still can\'t hear you[endrate] ? [rate="+50.00%"]why i can\'t hear you[endrate] ?';
$text = preg_replace('/\[pause\h*=\h*"(\d+)"]/', '<break time="$1ms"/>', $text);
echo $text;