java 字符串匹配函數(shù)
更新時(shí)間:2008年09月14日 20:50:06 作者:
java去掉字符串中匹配的字符串
去掉字符串中匹配 的字符串
/**
* 去掉字符串中匹配 的字符串
*
* @author zhujie
* @return String regex 要替換的內(nèi)容 value 字符串 state 替換的內(nèi)容變成什么
*/
public static String toRegex(String regex, String value, String state) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(value);
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, state);
}
m.appendTail(sb);
return sb.toString();
}
public static void main(String[] args) {
* String regex = "\\p{Lower}"; //去掉里面的小寫字符 regex ="'"; //去掉里面的 '
* regex="\\p{Sc}"; //去掉貨幣符號(hào) $ regex="\\p{Punct}"; //去掉里面的所有的符號(hào)
* regex="\""; //去掉 " regex ="<.>"; //去掉里面的html 標(biāo)簽
*
* String value="fjdks<sss>jfl'fdk$$jfl";
* System.out.println(toRegex(regex,value,""));
*/
}
復(fù)制代碼 代碼如下:
/**
* 去掉字符串中匹配 的字符串
*
* @author zhujie
* @return String regex 要替換的內(nèi)容 value 字符串 state 替換的內(nèi)容變成什么
*/
public static String toRegex(String regex, String value, String state) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(value);
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, state);
}
m.appendTail(sb);
return sb.toString();
}
復(fù)制代碼 代碼如下:
public static void main(String[] args) {
* String regex = "\\p{Lower}"; //去掉里面的小寫字符 regex ="'"; //去掉里面的 '
* regex="\\p{Sc}"; //去掉貨幣符號(hào) $ regex="\\p{Punct}"; //去掉里面的所有的符號(hào)
* regex="\""; //去掉 " regex ="<.>"; //去掉里面的html 標(biāo)簽
*
* String value="fjdks<sss>jfl'fdk$$jfl";
* System.out.println(toRegex(regex,value,""));
*/
}
相關(guān)文章
PHP preg_replace() 正則替換所有符合條件的字符串
PHP preg_replace() 正則替換,與Javascript 正則替換不同,PHP preg_replace() 默認(rèn)就是替換所有符號(hào)匹配條件的元素2014-02-02javascript 正則表達(dá)式(二) 使用技巧說明
javascript 正則表達(dá)式(二) 使用技巧說明2010-05-05你不一定知道的關(guān)于JavaScript的正則表達(dá)式
關(guān)于JavaScript中的正則表達(dá)式——,你不一定知道也可能用不到,但說不定哪天就會(huì)遭遇的幾個(gè)事實(shí)【新增一個(gè)】2010-09-09js正則表達(dá)式 限1-2位整數(shù),或者至多含有兩位小數(shù)的寫法
這篇文章主要介紹了js正則表達(dá)式,限1-2位整數(shù),或者至多含有兩位小數(shù),需要的朋友可以參考下2020-06-06