當前位置:首頁 » 編程語言 » java替換正則

java替換正則

發布時間: 2024-04-10 13:49:52

java正則表達式替換 比如 abbc 我想把第一個b替換成x,第二個b替換成o,用正則表達式該如何實現

String result, str = "abbc";
result = str.replaceFirst("b", "x");
result = str.replaceFirst("b", "o");
System.out.println("替換字元串結果:" + result);

replaceFirst("b", "o")這里的「b」其實就是正則式。

⑵ 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
declare@hr intege
declare@objRegExp integer
declare@result varchar(5000)exec@hr =sp_OACreate'VBScript.RegExp',@objRegExp OUTPUT
IF@hr <>0 begin
exec@hr =sp_OADestroy@objRegExp
returnnullend
exec@hr =sp_OASetProperty@objRegExp,'Pattern',@regexp
IF@hr <>0 begin
exec@hr =sp_OADestroy@objRegExp
returnnullend
exec@hr =sp_OASetProperty@objRegExp,'Global',@globalReplace
IF@hr <>0 begin
exec@hr =sp_OADestroy@objRegExp
returnnullend
exec@hr =sp_OASetProperty@objRegExp,'IgnoreCase',@ignoreCase
IF@hr <>0 begin
exec@hr =sp_OADestroy@objRegExp
returnnullend
exec@hr =sp_OAMethod@objRegExp,'Replace',@result OUTPUT,@source,@replace
IF@hr <>0 begin
exec@hr =sp_OADestroy@objRegExp
returnnullend
exec@hr =sp_OADestroy@objRegExp
IF@hr <>0 begin
returnnullend
return@result
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 replace 正則表達式

表達方式:
replace方法並不能改變字元串本身的,只是replace方法能返回一個處理後的字元串而已,例如:
String s="hello";
System.out.println(s.replace("h","H"));
System.out.println(s);
輸出結果:
Hello
hello
srcString.replaceAll("[\s()]","_");

\s表示一個空白符;
[\s()]表示 空格 左括弧 或者 右括弧中出現其中任何一個

replaceAll("[\s()]","_"); 即表示用_替代字元串中出現的空格符、左括弧、右括弧。

熱點內容
qtp錄制的腳本 發布:2024-05-05 01:14:04 瀏覽:365
如何安裝卡羅拉安卓系統 發布:2024-05-05 01:09:00 瀏覽:983
sql創建表查詢表 發布:2024-05-05 01:00:12 瀏覽:798
食色抖音上傳 發布:2024-05-05 00:55:56 瀏覽:656
java圖片下載 發布:2024-05-05 00:50:45 瀏覽:597
唱吧如何上傳伴奏 發布:2024-05-05 00:49:04 瀏覽:443
什麼配置單反拍視頻最好 發布:2024-05-05 00:30:56 瀏覽:478
sql敏感 發布:2024-05-05 00:28:20 瀏覽:62
android工程師筆試 發布:2024-05-05 00:10:52 瀏覽:948
python調試pycharm 發布:2024-05-05 00:10:51 瀏覽:707