一篇文章帶你了解Python和Java的正則表達(dá)式對(duì)比
參考資料:
簡(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é)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Python實(shí)現(xiàn)讀取Linux系統(tǒng)的CPU以及內(nèi)存占用
這篇文章主要為大家詳細(xì)介紹了如何利用Python語(yǔ)言實(shí)現(xiàn)Linux系統(tǒng)的CPU以及內(nèi)存占用,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,需要的可以收藏一下2023-05-05Django跨域請(qǐng)求原理及實(shí)現(xiàn)代碼
這篇文章主要介紹了Django跨域請(qǐng)求原理及實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11Python利用pandas和matplotlib實(shí)現(xiàn)繪制雙柱狀圖
在數(shù)據(jù)分析和可視化中,常用的一種圖形類型是柱狀圖,柱狀圖能夠清晰地展示不同分類變量的數(shù)值,并支持多組數(shù)據(jù)進(jìn)行對(duì)比,本篇文章將介紹python如何使用pandas和matplotlib繪制雙柱狀圖,需要的可以參考下2023-11-11Python實(shí)現(xiàn)統(tǒng)計(jì)英文文章詞頻的方法分析
這篇文章主要介紹了Python實(shí)現(xiàn)統(tǒng)計(jì)英文文章詞頻的方法,結(jié)合實(shí)例形式分析了Python針對(duì)英文單詞頻率統(tǒng)計(jì)的相關(guān)原理、實(shí)現(xiàn)方法及具體操作技巧,需要的朋友可以參考下2019-01-01簡(jiǎn)單掌握Python的Collections模塊中counter結(jié)構(gòu)的用法
counter數(shù)據(jù)結(jié)構(gòu)被用來(lái)提供技術(shù)功能,形式類似于Python中內(nèi)置的字典結(jié)構(gòu),這里通過(guò)幾個(gè)小例子來(lái)簡(jiǎn)單掌握Python的Collections模塊中counter結(jié)構(gòu)的用法:2016-07-07Python3.5內(nèi)置模塊之shelve模塊、xml模塊、configparser模塊、hashlib、hmac模塊用法
這篇文章主要介紹了Python3.5內(nèi)置模塊之shelve模塊、xml模塊、configparser模塊、hashlib、hmac模塊,結(jié)合實(shí)例形式較為詳細(xì)的分析了shelve、xml、configparser、hashlib、hmac等模塊的功能及使用方法,需要的朋友可以參考下2019-04-04