㈠ java正則表達式匹配img標簽並替換
publicstaticvoidmain(String[]args){
Stringxx="hdjksahdjkshjkhkjdhsakj<imgsrc="/ee/ads/blll/1.0.0/img/defineform.png"/>dsadsajkdas";
Stringregex="src="/ee/ads/blll/1.0.0/img/defineform.png"";
xx=xx.replaceAll(regex,"tttt");
System.out.println(xx);
}
㈡ java正則表達式,怎麼替換指定字元串為 指定字元+原字元串
String text = "ABCDE";
String regex = "([A-Z])";//這里的()表示保存匹配的結果
System.out.println(text.replaceAll(regex,"\\\\$1")); //$1取出保存的第1個
㈢ java正則表達式取值並保持格式替換
import java.util.regex.*;
public class Exam
{
public static void main(String[] args)
{
final String s="t1:\"1:0\",t2:\"2:0\",t3:\"2:1\",t4:\"2:2\"",d;
Pattern p=Pattern.compile("(\\d):(\\d)");
Matcher m=p.matcher(s);
// System.out.println(s);
d=m.replaceAll("s$1$2");
System.out.println(d);
}
}
㈣ java正則表達式 如何匹配 | 或者是如何替換 |
"1|2".replaceAll("\\|","a");
瀏覽器裡面快速驗證:
javascript:alert("1|2".replace(/\|/,"a"));
㈤ JAVA正則表達式替換字元串問題
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;
publicclassTestRegex
{
publicstaticvoidmain(String[]args)
{
Stringregex="第[0-9]*條";
Stringstr="第9條,數據錯誤,錯誤信息,第jjj哦條哦條我的條件如何?第221條xx";
Patternpat=Pattern.compile(regex);
Matchermatcher=pat.matcher(str);
while(matcher.find()){
Stringtemp=str.substring(matcher.start(),matcher.end());
str=str.replaceAll(temp,temp.substring(0,temp.lastIndexOf("條"))+"行");
}
System.out.println(str);
}
}
㈥ java正則表達式替換一段字元串
Java正則表達式 .*(from.*)$ 替換成 select count(*) $1
完整的Java替換程序如下
publicclassAA{
publicstaticvoidmain(String[]args){
Strings="Selectafromxxxa"+"wherea.id=:id";
Stringregex=".*(from.*)$";
Stringresult=s.replaceAll(regex,"selectcount(*)$1");
System.out.println(result);
}
}
運行結果
selectcount(*)fromxxxawherea.id=:id
因為我不知道TbItem.class.getName()方法返回的表名,所以用xxx代替.
你可以用Strings="Selectafrom"+TbItem.class.getName()+"a"+"wherea.id=:id";沒問題不用改.
㈦ java正則表達式查找替換字元串怎麼寫
我給你一個把數字前面的0去掉(中間和尾部的0保留)的Java正則表達式替換字元串的程序.
publicclassTest{
publicstaticvoidmain(String[]args){
Strings="000120440";
System.out.println(s.replaceAll("^[0]+",""));
}
}
運行結果
120440
㈧ java想替換字元,正則表達式該怎麼寫
/**
* 在String對象上添加trim方法
*/
function String.prototype.trim()
{
return this.replace(/(^\s*)|(\s*$)/g,"");
}
㈨ java正則表達式怎麼一個個替換匹配的內容
Stringa="我是f_static_000的f_static_001aaaf_static_001";
//正則根據自己需要修改,replaceAll可以使用正則的捕獲組功能,$n引用第n個捕獲組
/**
replaceAll(regExp,replacement);第一個參數是正則字元串,第二個是替換內容
正則裡面有捕獲(正則裡面用小括弧捕獲)和引用的功能
*/
a=a.replaceAll("(f_static_\d+)","#[face/png/$1.png]#");