为什么我的正则表达式拒绝撇号?

时间:2021-10-06 22:30:11

I'm making a regex which should match everything like that : [[First example]] or [[I'm an example]].

我正在制作一个正则表达式,它应该匹配所有类似的东西:[[第一个例子]]或[[我是一个例子]]。

Unfortunately, it doesn't match [[I'm an example]] because of the apostrophe.

不幸的是,由于撇号,它与[[我是一个例子]]不匹配。

Here it is :

这里是 :

preg_replace_callback('/\[\[([^?"`*%#\\\\:<>]+)\]\]/iU', ...)

Simple apostrophes (') are allowed so I really do not understand why it doesn't work.

允许使用简单的撇号('),所以我真的不明白为什么它不起作用。

Any ideas ?

有任何想法吗 ?

EDIT : Here is what's happening before I'm using this regex

编辑:这是在我使用这个正则表达式之前发生的事情

// This match something [[[like this]]]
$contents = preg_replace_callback('/\[\[\[(.+)\]\]\]/isU',function($matches) {
    return '<blockquote>'.$matches[1].'</blockquote>';
}, $contents);

// This match something [[like that]] but doesn't work with apostrophe/quote when
// the first preg_replace_callback has done his job
$contents = preg_replace_callback('/\[\[([^?"`*%#\\\\:<>]+)\]\]/iU', ..., $contents);

2 个解决方案

#1


try this:

$string = '[[First example]]';
$pattern = '/\[\[(.*?)\]\]/';
preg_match ( $pattern, $string, $matchs );
var_dump ( $matchs );

#2


You can use this regex:

你可以使用这个正则表达式:

\[\[.*?]]

Working demo

Php code

$re = '/\[\[.*?]]/'; 
$str = "not match this but [[Match this example]] and not this"; 

preg_match_all($re, $str, $matches);

Btw, if you want to capture the content within brackets you have to use capturing groups:

顺便说一句,如果要捕获括号内的内容,则必须使用捕获组:

\[\[(.*?)]]

#1


try this:

$string = '[[First example]]';
$pattern = '/\[\[(.*?)\]\]/';
preg_match ( $pattern, $string, $matchs );
var_dump ( $matchs );

#2


You can use this regex:

你可以使用这个正则表达式:

\[\[.*?]]

Working demo

Php code

$re = '/\[\[.*?]]/'; 
$str = "not match this but [[Match this example]] and not this"; 

preg_match_all($re, $str, $matches);

Btw, if you want to capture the content within brackets you have to use capturing groups:

顺便说一句,如果要捕获括号内的内容,则必须使用捕获组:

\[\[(.*?)]]