本文目錄一覽:
java正則表達式怎麼一個個替換匹配的內容
String a = “我是 f_static_000 的 f_static_001 aaa f_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的替換同理。
public class Demo {
public static void main(String[] args) {
String str = “aa文字1bb哈哈cc測試dx,測試字元串aa1234bb”;
// 替換aa、bb之間的字元串為 “成功”
String str1 = str.replaceAll(“aa.*?bb”, “aa成功bb”);
System.out.println(str1);
// 替換aa、bb之間的字元串為 “成功”
String str2 = str.replaceAll(“(aa).*?(bb)”, “$1成功$2”);
System.out.println(str2);
// 替換小寫字母之間的字元串為 “成功”
String str3 = str.replaceAll(“([a-z]+).*?([a-z]+)”, “$1成功$2”);
System.out.println(str3);
}
}
java正則表達式替換一段字元串
Java正則表達式 .*(from.*)$ 替換成 select count(*) $1
完整的Java替換程序如下
public class AA {
public static void main(String[] args) {
String s=” Select a from xxx a ” + ” where a.id= :id”;
String regex = “.*(from.*)$”;
String result=s.replaceAll(regex,”select count(*) $1″);
System.out.println(result);
}
}
運行結果
select count(*) from xxx a where a.id= :id
因為我不知道TbItem.class.getName()方法返回的表名,所以用xxx代替.
你可以用String s=” Select a from ” + TbItem.class.getName() + ” a ” + ” where a.id= :id”;沒問題不用改.
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 procedure ‘sys.sp_OACreate’ of component ‘Ole Automation Procedures’ 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 Procedures’ by using sp_configure. For more information about enabling ‘Ole Automation Procedures’, see “Surface Area Configuration” in SQL Server Books Online.
這是因為未開啟Ole Automation Procedures選項,MSDN中的Ole Automation Procedures選項。執行下面的語句開啟這個選項:sp_configure’show advanced options’,1;GO
RECONFIGURE;GOsp_configure’Ole Automation Procedures’,1;GO
RECONFIGURE;GO
所有的準備工作都已經做好,那就試驗一下吧。
Example1:忽略大小寫並替換selectdbo.regexReplace(‘123456′,’]*[^’,”,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,’]*(.|\n)*?’,”,1,1)
Example3:去除html標籤selectdbo.regexReplace(‘
Key Contact:
Mr Jack, Zhou
General Manager
Mr Adu, Ho
Marketing Director
Overseas Sales
MsWinny, Luo
Sales Manager
Overseas Sales’,’]*’,”,1,0)
Example4:資料庫欄位值替換updateBooks。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/277071.html