如何 preg_replace 括号内的特定字符?
我得到了一堆像这样的字符串:
test; test2 (2;5%).
我现在想要用 PHP 做的是纠正“;”在括号中,所以它看起来像:
test; test2 (2,5%) - mention the ",".
我尝试过的代码:
$string = preg_replace("/\(.;.\)/", ",", $string);
我的眼睛实际上因谷歌搜索而流血,所以请帮我解决这个问题:)
I got a bunch of strings like this one:
test; test2 (2;5%).
What I want to do with PHP now is to correct the ";" in the parentheses so it will look like:
test; test2 (2,5%) - mention the ",".
Code that I tried:
$string = preg_replace("/\(.;.\)/", ",", $string);
My eyes actually bleed from googling so please help me out with that :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
1) 需要用反斜杠 (
\
) 转义括号2) 表达式中没有包含百分号
3) 本来可以用单个 < 替换整个括号表达式(包括括号) code>,
4) 正如 Zimzat 在评论中指出的那样,用
\d+
替换句点 (.
) 可能是一个更好的主意,它匹配数字而不是任何字符。1) Needed to escape the parenthesis with a backslash (
\
)2) Didn't include the percent sign in your expression
3) Would have been replacing the entire bracketed expression (brackets included) with a single
,
4) As Zimzat points out in the comments, it might be a better idea to replace the periods (
.
) with\d+
, which matches numbers instead of any character.您必须捕获
;
旁边的字符:You have to capture the characters next to the
;
:要替换 () 中多次出现的例如逗号,我将使用此代码
to replace multiple occurences of eg comma in () i will use this code