一篇文章帶你了解Python和Java的正則表達(dá)式對(duì)比
發(fā)布日期:2022-01-03 22:36 | 文章來源:站長(zhǎng)之家
參考資料:
- 正則表達(dá)式語法–菜鳥教程
- Java正則表達(dá)式實(shí)現(xiàn)
簡(jiǎn)單批量替換
舉例:將and 批量替換為&&
Python實(shí)現(xiàn)
import re def transformSimple(fromRegex, toText, inText): return re.sub(fromRegex, toText,inText, flags =re.I) if __name__ == "__main__": inText = "x =1 and y =2" fromRegex = " and " toText = " && " outText = transformSimple(fromRegex,toText,inText ) print(outText) ## OUTPUT: x =1 && y =2
Java實(shí)現(xiàn)
import java.util.*;
import java.util.regex.*;
public class RegexTest {
private static String transformSimple(String regexPattern, String replText, String inText){
return Pattern.compile(regexPattern, Pattern.CASE_INSENSITIVE).matcher(inText).replaceAll(replText);
}
public static void main(String[] args) {
String input = "x =1 and y =2";
String patternString =" and ";
String toText = " && ";
String outText ="";
outText = transformSimple(patternString, toText, input);
System.out.println("RESULT: " + outText);
}
// RESULT: x =1 && y =2
復(fù)雜模板替換
舉例:將x in (1,2)批量替換為[1,2].contains(x)
分析: 模板化
- 輸入分組捕獲
(\S+)\s+in\s*\((.+?)\) - 輸出分組填寫
[@2].contains(@1) – @1和@2分別對(duì)應(yīng)分組捕獲中的第1組和2組。
Python實(shí)現(xiàn)
import re
def transformComplex(fromRegex, toText, inText):
regObj = re.compile(fromRegex, flags =re.I)
for match in regObj.finditer(inText):
index = 1
outText = toText
for group in match.groups():
outText = outText.replace("@"+str(index), group)
index +=1
inText = inText.replace(match.group(0), outText)
return inText
if __name__ == "__main__":
fromRegex = "(\S+)\s+in\s*\((.+?)\)"
toText = "[@2].contains(@1)"
inText = "x in (1,2) and y in (3,4)"
outText22 = transformComplex(fromRegex, toText, inText)
print(outText22)
## OUTPUT: [1,2].contains(x) and [3,4].contains(y)
Java實(shí)現(xiàn)
import java.util.*;
import java.util.regex.*;
public class RegexTest {
private static String transformComplex(String regexPattern, String replText, String inText){
Pattern pattern = Pattern.compile(regexPattern, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inText);
String outText ="";
while (matcher.find()){
outText = replText;
for (int i =1; i <= matcher.groupCount(); i++){
outText = outText.replace("@"+i, matcher.group(i));
}
inText = inText.replace(matcher.group(0), outText);
}
return inText;
}
public static void main(String[] args) {
String input = "x in (1,2) and y in (3,4)";
String patternString ="(\\S+)\\s+in\\s*\\((.+?)\\)";
String toText = "[@2].contains(@1)";
String outText ="";
outText = transformComplex(patternString, toText, input);
System.out.println("RESULT: " + outText);
}
}
// RESULT: [1,2].contains(x) and [3,4].contains(y)
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注本站的更多內(nèi)容!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
相關(guān)文章
關(guān)注官方微信