溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python和Java的正則表達(dá)式對比

發(fā)布時間:2021-09-14 17:30:48 來源:億速云 閱讀:415 作者:chen 欄目:開發(fā)技術(shù)

這篇文章主要講解了“Python和Java的正則表達(dá)式對比”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Python和Java的正則表達(dá)式對比”吧!

目錄
  • 簡單批量替換

  • 復(fù)雜模板替換

  • 總結(jié)

簡單批量替換

舉例:將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分別對應(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)

感謝各位的閱讀,以上就是“Python和Java的正則表達(dá)式對比”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對Python和Java的正則表達(dá)式對比這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI