快速說明:我知道降價(jià)解析器不關(guān)心這個(gè)問題。這是為了 md 文件中的視覺一致性以及實(shí)驗(yàn)。樣品:# this##that###or this other目標(biāo):閱讀每一行,如果 Markdown 標(biāo)題在井號(hào)/主題標(biāo)簽后面沒有空格,則添加一個(gè),使其看起來像:# this## that### or this other我的非正則表達(dá)式嘗試: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這有效,但它沒有機(jī)制來匹配七個(gè)“#”的不太可能的情況。無論功效如何,我都想弄清楚如何在 php 中使用正則表達(dá)式(如果重要的話,也可能是 javascript)。
2 回答

臨摹微笑
TA貢獻(xiàn)1982條經(jīng)驗(yàn) 獲得超2個(gè)贊
我猜一個(gè)帶有正確字符列表邊界的簡單表達(dá)式可能在這里工作,也許:
(#)([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 關(guān)注
- 159 瀏覽
添加回答
舉報(bào)
0/150
提交
取消