本文介绍了正则表达式匹配除尾随空格之外的任何字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用这个 /\([^\S\n]*(.*)[^\S\n]*\)/
正则表达式来匹配括号内的内容,并且它工作得很好,除非有尾随空格,它会匹配它们.
I'm using this /\([^\S\n]*(.*)[^\S\n]*\)/
regex to match what is inside brackets, and it works good except when there are trailing spaces, it matches them.
例如 ( test1 test2 )
我想匹配 test1 test2
,但我匹配 test1 test2_
(我写了下划线,但它是尾随空格).
In for example ( test1 test2 )
I would like to match test1 test2
, but I match test1 test2_
(I wrote underscore, but it's trailing space).
知道如何从我的比赛中删除这个尾随空格吗?
Any idea how to remove this trailing space from my match?
我正在使用 PHP preg_replace 函数.
I'm using PHP preg_replace function.
推荐答案
试试这个
/\(\s*([^)]+?)\s*\)/
结果:
$reg = '/\(\s*([^)]+?)\s*\)/';
var_dump( preg_replace( $reg, '$1', "( test1 test2 )" ) );
//string(11) "test1 test2"
这篇关于正则表达式匹配除尾随空格之外的任何字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!