Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Regex替换周围的字符,同时保持_Php_Regex_String_Preg Replace_Preg Match - Fatal编程技术网

Php Regex替换周围的字符,同时保持

Php Regex替换周围的字符,同时保持,php,regex,string,preg-replace,preg-match,Php,Regex,String,Preg Replace,Preg Match,我正在使用PHP尝试将文本从一种风格的Markdown转换为另一种风格的Markdown 例如,如果我有字符串**some text**,则应将其替换为字符串''some text'(每侧的**替换为'''triple撇号)。但是,字符串**some other text不应进行任何替换,因为它不以** 目前,我正在使用以下代码: function convertBoldText($line){ #Regex replace double asterisk IF if is FOLLOW

我正在使用PHP尝试将文本从一种风格的Markdown转换为另一种风格的Markdown

例如,如果我有字符串
**some text**
,则应将其替换为字符串
''some text'
(每侧的**替换为'''triple撇号)。但是,字符串
**some other text
不应进行任何替换,因为它不以
**

目前,我正在使用以下代码:

function convertBoldText($line){
    #Regex replace double asterisk IF if is FOLLOWED by a non-asterisk character
    $tmp = preg_replace('/\*{2}(?=[^\*])/', "'''", $line);
    #Regex replace double asterisk IF if is PRECEDED by a non-asterisk character
    return preg_replace('/(?<=[^\*])\*{2}/', "'''", $tmp);
  }
函数convertBoldText($line){
#如果后跟非星号字符,则用正则表达式替换双星号
$tmp=preg\u replace('/\*{2}(?=[^\*])/',“'''''”,$line);
#如果前面有非星号字符,则用正则表达式替换双星号

返回preg_replace('/(?您可以使用正则表达式来匹配
**
,该正则表达式后跟任何文本,但
**
,然后后跟
**

 function convertBoldText($line){
return preg_replace('/\*{2}(?!\s)((?:(?!\*{2}).)*)(?<!\s)\*{2}/s', "'''$1'''", $line);

为什么不替换此-
**这些第一个星号不应该被替换**
?这对我来说很有效,而且老实说,这是一个比我预期的简单得多的答案。谢谢!非常有效。
return preg_replace('/\*{2}(?!\s)([^*]*(?:\*(?!\*)[^*]*)*)(?<!\s)\*{2}/', "'''$1'''", $line);