快速說明:我知道降價解析器不關心這個問題。這是為了 md 文件中的視覺一致性以及實驗。樣品:# this##that###or this other目標:閱讀每一行,如果 Markdown 標題在井號/主題標簽后面沒有空格,則添加一個,使其看起來像:# this## that### or this other我的非正則表達式嘗試:function inelegantFunction (string $string){ $array = explode('#',$string); $num = count($array); $text = end($array); return str_repeat('#', $num-1)." ".$text;}echo inelegantFunction("###or this other"); // returns ### or this other這有效,但它沒有機制來匹配七個“#”的不太可能的情況。無論功效如何,我都想弄清楚如何在 php 中使用正則表達式(如果重要的話,也可能是 javascript)。
2 回答

臨摹微笑
TA貢獻1982條經驗 獲得超2個贊
我猜一個帶有正確字符列表邊界的簡單表達式可能在這里工作,也許:
(#)([a-z])
如果我們可能有更多字符,我們可以簡單地將它添加到[a-z]
.
測試
$re = '/(#)([a-z])/m';
$str = '#this
##that
###that
### or this other';
$subst = '$1 $2';
$result = preg_replace($re, $subst, $str);
echo "The result of the substitution is ".$result;
- 2 回答
- 0 關注
- 167 瀏覽
添加回答
舉報
0/150
提交
取消