简体   繁体   English

php中的regexp对于@ \\\\ section {(。*?)} @ U不起作用

[英]regexp in php doesn't work for @\\section{(.*?)}@U

I wrote a simple regex-script in PHP: 我用PHP编写了一个简单的正则表达式脚本:

<?php  
$string = "\section{Test 1} 
sdfgsdfg";  
$regex = "@\\section{(.*?)}@U";  
$replace = "$1jksdfahlkjh";  
$newString = preg_replace ($regex, $replace, $string, -1 );  
?> 

See: http://regexp-tester.mediacix.de/t287 请参阅: http//regexp-tester.mediacix.de/t287

using 使用

echo $newString;

only give me 只给我

\\section{Test 1} sdfgsdfg \\ section {测试1} sdfgsdfg

Regex-patterns like 正则表达式模式如

$bbcode = array(

"/\[b\](.*?)\[\/b\]/is" => "<strong>$1</strong>",
"/\[u\](.*?)\[\/u\]/is" => "<u>$1</u>",
"/\[url\=(.*?)\](.*?)\[\/b\]/is" => "<a href='$1'>$2</a>"

);
$text = "[b]Text[/b][u]Text[/u]";

$text = preg_replace(array_keys($bbcode), array_values($bbcode), $text);
echo $text;

works great. 效果很好。 Is there any solution? 有什么解决办法吗? I don't want to write an whole LaTeX-Parser, but I want to replace those "\\section{...}"-Strings 我不想编写整个LaTeX-Parser,但是我想替换那些“ \\ section {...}”-Strings

You need to escape the \\ 's, because otherwise they will already be treated as an escape by php itself and then stripped: 您需要转义\\ ,因为否则它们将被php本身视为转义符,然后被剥离:

$regexp = "@\\\\section{(.*)}@";

The U modifier is unnecessary, as that makes the .*? U修饰符是不必要的,因为这会使.*? greedy again, so you are reversing the reversing. 再次贪婪,所以您正在反转。 Just .* works just as well. 只是.*也一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM