An article shows you the comparison of regular expressions between Python and Java

  • 2021-11-30 00:43:03
  • OfStack

Catalog Simple Batch Replacement Complex Template Replacement Summary

References:

Regular expression syntax-a rookie tutorial Implementation of Java Regular Expression

Simple batch replacement

Example: Will and Bulk replace with &&

Python Implementation


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 Implementation


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

Complex template replacement

Example: Will x in (1,2) Bulk replace with [1,2].contains(x)

Analysis: Templating

Input packet capture (\S+)\s+in\s*\((.+?)\) Output grouping filling [@2].contains(@1) – @1 And @ 2 correspond to Group 1 and Group 2 in packet capture, respectively.

Python Implementation


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)

Implementation of Java


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)

Summarize

This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!


Related articles: