[英]java how to replace N number of spaces using regular expression
我喜歡在網頁中很好地顯示文本(在textArea中輸入文本)。 所以需要
1. change linebreaker \n to <br/>
2. keep indentation:
replace 2 spaces with one space and
replace 3 spaces with one space and 2
replace 4 spaces with one space and 3
replace N spaces with one space and N-1
有沒有辦法在JAVA中使用正則表達式替換空格? 謝謝。
下划線表示空格:
查找: (?<=_)_
替換:
如果你堅持使用正則表達式, 正回顧后是你在找什么。
基本上,這將匹配緊隨另一個空間的每個空間。
這應該可以幫助您:
String oldString="";
String newString = oldString.replaceAll("\n", "<br />").replaceAll("(?<= ) ", " ");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.