正则表达式查找用自定义符号括起来的单词或句子
Regex to find the words or sentences that enclosed with custom sign
我需要替换一些单词或一些句子并将其转换为下划线。我正在使用 PHP 并找到一个参考:PHP Regex, extract all custom tags from text
不知何故,案例只涵盖单个单词而不涵盖句子。如何使正则表达式也可以捕获 ##
标签所包含的所有内容?
假设我的输入如下:
"ll the Lorem Ipsum ##generators## on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over ##200 Latin words##, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always ##free from repetition##, injected humour, or non-characteristic words etc."
那么输出将是:
"ll the Lorem Ipsum ____1____ on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over ____2____, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always ____3____, injected humour, or non-characteristic words etc."
任何人都可以帮助我如何获得正则表达式模式吗?
我认为正则表达式是:
/##[^#]+##/g
$text = 'll the Lorem Ipsum ##generators## on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over ##200 Latin words##, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always ##free from repetition##, injected humour, or non-characteristic words etc.';
preg_match_all('/##[^#]+##/', $text, $matches, PREG_SET_ORDER);
for ($i = 0; $i < count($matches); $i++) {
$text = preg_replace("/".$matches[$i][0]."/", "___".strval($i+1)."___" , $text, 1);
}
使用@shA.t 的正则表达式的另一个想法是将preg_replace_callback
与递增变量的函数一起使用。所以可以不用循环,这样可能会提高一点效率。
$str = preg_replace_callback('/##[^#]+##/', function($m) use (&$i) {
return "____". ++$i ."____";
}, $str);
我需要替换一些单词或一些句子并将其转换为下划线。我正在使用 PHP 并找到一个参考:PHP Regex, extract all custom tags from text
不知何故,案例只涵盖单个单词而不涵盖句子。如何使正则表达式也可以捕获 ##
标签所包含的所有内容?
假设我的输入如下:
"ll the Lorem Ipsum ##generators## on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over ##200 Latin words##, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always ##free from repetition##, injected humour, or non-characteristic words etc."
那么输出将是:
"ll the Lorem Ipsum ____1____ on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over ____2____, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always ____3____, injected humour, or non-characteristic words etc."
任何人都可以帮助我如何获得正则表达式模式吗?
我认为正则表达式是:
/##[^#]+##/g
$text = 'll the Lorem Ipsum ##generators## on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over ##200 Latin words##, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always ##free from repetition##, injected humour, or non-characteristic words etc.';
preg_match_all('/##[^#]+##/', $text, $matches, PREG_SET_ORDER);
for ($i = 0; $i < count($matches); $i++) {
$text = preg_replace("/".$matches[$i][0]."/", "___".strval($i+1)."___" , $text, 1);
}
使用@shA.t 的正则表达式的另一个想法是将preg_replace_callback
与递增变量的函数一起使用。所以可以不用循环,这样可能会提高一点效率。
$str = preg_replace_callback('/##[^#]+##/', function($m) use (&$i) {
return "____". ++$i ."____";
}, $str);