java正則替換
Ⅰ java 正則表達式 怎麼用正則表達式替換括弧里內容的同時去掉圓括弧
按照你的要求編寫的Java替換程序如下
public class R{
public static void main(String[] args) {
String s = "(我)發來了一條信息。(你好。)";
s=s.replaceAll("(我)","小明");
s=s.replaceAll("(你好。)","晚安。");
System.out.println(s);
}
}
Ⅱ 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]#");
Ⅲ java正則表達式怎麼定義只替換中間的字元
可以使用分組來完成,替換字元串中使用$1、$2、$3……可以獲取對應組的匹配結果。如果前後的字元串是固定的那更簡單,直接在替換的時候寫上就好了。
因為String的replaceAll就是使用的正則表達式所以示例直接使用的String的替換,Pattern的替換同理。
publicclassDemo{
publicstaticvoidmain(String[]args){
Stringstr="aa文字1bb哈哈cc測試dx,測試字元串aa1234bb";
//替換aa、bb之間的字元串為"成功"
Stringstr1=str.replaceAll("aa.*?bb","aa成功bb");
System.out.println(str1);
//替換aa、bb之間的字元串為"成功"
Stringstr2=str.replaceAll("(aa).*?(bb)","$1成功$2");
System.out.println(str2);
//替換小寫字母之間的字元串為"成功"
Stringstr3=str.replaceAll("([a-z]+).*?([a-z]+)","$1成功$2");
System.out.println(str3);
}
}
Ⅳ java過濾sql關鍵字的正則替換掉
java過濾sql關鍵字的正則替換掉方法如下:
可以在C#中這樣做:Regexregex = newRegex(@"]*>[^");
stringcleanedHtml = regex.Replace(html, "");
可是我並不想再寫個循環去遍歷每條記錄,然後保存每條記錄,我想在資料庫中一步到位,而sql只提供了簡單的replace函數,這個函數明顯不能達到咱的要求,那就去寫一個自定義函數吧。
函數源代碼如下:CREATE functiondbo.regexReplace
(@source ntext,--原字元串@regexp varchar(1000),--正則表達式@replace varchar(1000),--替換值@globalReplace bit=1,--是否是全局替換@ignoreCase bit=0 --是否忽略大小寫)returnS varchar(1000)AS
begin
[email protected] intege
[email protected] integer
[email protected] varchar(5000)[email protected] =sp_OACreate'VBScript.RegExp',@objRegExp OUTPUT
[email protected] <>0 begin
[email protected] [email protected]
returnnullend
[email protected] =[email protected],'Pattern',@regexp
[email protected] <>0 begin
[email protected] [email protected]
returnnullend
[email protected] [email protected],'Global',@globalReplace
[email protected] <>0 begin
[email protected] [email protected]
returnnullend
[email protected] [email protected],'IgnoreCase',@ignoreCase
[email protected] <>0 begin
[email protected] [email protected]
returnnullend
[email protected] [email protected],'Replace',@result OUTPUT,@source,@replace
[email protected] <>0 begin
[email protected] [email protected]
returnnullend
[email protected] [email protected]
[email protected] <>0 begin
returnnullend
[email protected]
end
需要注意的是,即使寫好了這個函數,也並不能馬上使用。執行這個函數時可能會出現以下的錯誤:Msg 15281, Level 16, State 1, Line 1
SQL Server blocked access to procere 'sys.sp_OACreate' of component 'Ole Automation Proceres' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Ole Automation Proceres' by using sp_configure. For more information about enabling 'Ole Automation Proceres', see "Surface Area Configuration" in SQL Server Books Online.
這是因為未開啟Ole Automation Proceres選項,MSDN中的Ole Automation Proceres選項。執行下面的語句開啟這個選項:sp_configure'show advanced options',1;GO
RECONFIGURE;GOsp_configure'Ole Automation Proceres',1;GO
RECONFIGURE;GO
所有的准備工作都已經做好,那就試驗一下吧。
Example1:忽略大小寫並替換selectdbo.regexReplace(',']*>[^','',1,1)
Example2: 使用貪婪匹配
html代碼:
Also Available - Smith & Hogan: Criminal Law Cases & Materials 10th ed
There is, as ever, detailed analysis of the many recent case developments, in particular,
a revision of the chapter dealing with secondary liability and joint enterprise.
調用代碼:selectdbo.regexReplace(html,']*>(.|
)*?','',1,1)
Example3:去除html標簽selectdbo.regexReplace('
Key Contact:
Mr Jack, Zhou
General Manager
Mr A, Ho
Marketing Director
Overseas Sales
MsWinny, Luo
Sales Manager
Overseas Sales',']*>','',1,0)
Example4:資料庫欄位值替換updateBooks。
Ⅳ Java 正則表達式 替換字元串中人名
public static void main(String[] args) {
System.out.println("請輸入姓名:");
Scanner in=new Scanner(System.in);
String input=in.nextLine();
//保留姓氏
char str2=input.charAt(0);
//截取名字
String str1=input.substring(1);
//用正則表達式替換(包括漢字,數字,大小寫字母)
str1=str1.replaceAll("[^x00-xff]|\w", "x");
//輸出替換後的名字
System.out.println(str2+str1);
}
Ⅵ java正則表達式多個字元串替換怎樣實現
str.replaceAll("[A-Z]+\\d{3}", "hello").replaceAll("[^A-Za-z]", "==");
Ⅶ JAVA 替換特殊字元 的 正則表達式
JAVA替換特殊字元的正則表達式
代碼如下:
importjava.util.regex.*;
//表達式對象
Patternp=Pattern.compile("[\'\*\~]");
//創建Matcher對象
Matcherm=p.matcher("Stringstr="t'e*s~t";");
//替換
Stringnewstring=m.replaceAll("[$0]");
效果如下:
正則參考http://zh.wikipedia.org/wiki/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F
Ⅷ java 正則替換 \(
你是要把\(替換成什麼,既然沒說替換成為什麼,我以把出現"\("的地方替換為""為例
String oldStr = "123\\(abc";
System.out.println(oldStr);
String newStr = oldStr.replaceAll("\\\\\\(", "");
System.out.println(newStr);
Ⅸ java使用正則表達式替換字元串
正則改成這個: (?<=\\b)\\W*/r(?=\\b)
Ⅹ JAVA 正則替換字元
String s = "<A class=test id=\"123\" name=張三 tt=kk()>這里不替換 test=no kakaka</A><C class=sss /><B class=a id=\"b\" name=c></B>";
System.out.println(s);
System.out.println("------------------------------------------");
String s1 = s.replaceAll("=([^'\">\\s]+)(\\s|>)","=\"$1\"$2");
System.out.println(s1);
//----------------------------------------
輸出的結果是
<A class=test id="123" name=張三 tt=kk()>這里不替換 test=no kakaka</A><C class=sss /><B class=a id="b" name=c></B>
------------------------------------------
<A class="test" id="123" name="張三" tt="kk()">這里不替換 test="no" kakaka</A><C class="sss" /><B class="a" id="b" name="c"></B>
為了非標簽內的=號,可能得使用預查找,但測試有點問題就暫時不貼上來了,如果要求不是很嚴格,這個是可以湊活著用的
//=====和樓主討論中發現一個奇怪問題,就是[^'\">\\s]不能正確匹配到中文(樓主的jdk1.4就會漏掉中文)。
經過測試,在1.4以及以後的版本,下面這個是可行的
String s1 = s.replaceAll("=(?!['\"])(.*?)(\\s|>)","=\"$1\"$2");