亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

檢測并替換不成對的 Markdown 符號,而不更改成對的符號

檢測并替換不成對的 Markdown 符號,而不更改成對的符號

翻過高山走不出你 2023-07-19 10:58:48
我有這個Hello \**how are you\**? I'm fine*需要得到這個Hello *how are you*? I'm fine\*我可以得到Hello *how are you*? I'm fine*但后來我迷路了,因為s.replace("*', "\*")這不是一個選擇基本上問題是匹配的(配對的) * 需要用 * 替換,而未配對的 * 需要轉義。
查看完整描述

2 回答

?
慕婉清6462132

TA貢獻1804條經驗 獲得超2個贊

基本思想是將字符串拆分為單詞,然后找出哪些單詞具有未配對的“*”。


String text1 = "*This* is **my** text*";

String[] words = text1.split(" ");

for(int i=0; i<words.length; i++){

    int count = words[i].length() - words[i].replace("*", "").length(); // count the number of '*'

    if(count%2 != 0){ // if it's not paired we can replace with '\*'

        words[i] = words[i].replace("*", "\\*");

    }

}

System.out.println(String.join(" ", words));


Which prints out: *This* is **my** text\*


查看完整回答
反對 回復 2023-07-19
?
慕尼黑的夜晚無繁華

TA貢獻1864條經驗 獲得超6個贊

有人幫我解決了這個問題:


String text1 = "*This is **my** text* ";

System.out.println(text1.replaceAll("(?<=[^*\\\\]|\\A)[*](?=[^*\\\\]|\\z)", "\\\\*"));

打印\*This is **my** text\*


查看完整回答
反對 回復 2023-07-19
  • 2 回答
  • 0 關注
  • 172 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號