一篇文章带你了解Python和Java的正则表达式对比

网友投稿 291 2022-09-27


一篇文章带你了解Python和Java的正则表达式对比

目录简单批量替换复杂模板替换总结

参考资料:

正则表达式语法–菜鸟教程

java正则表达式实现

简单批量替换

举例:将and 批量替换为&&

python实现

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 = " && "RtGPV

outText = transformSimple(fromRegex,toText,inText )

print(outText)

## OUTPUT: x =1 && y =2

Java实现

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

复杂模板替换

举例:将x in (1,2)批量替换为[1,2].contains(x)

分析: 模板化

输入分组捕获 (\S+)\s+in\s*\((.+?)\)

输出分组填写 [@2].contains(@1) – @1和@2分别对应分组捕获中的第1组和2组。

Python实现

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*\((.+?)\)"

RtGPV 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实现

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)

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注我们的更多内容!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:GNS3全面详解系列-GNS3的前世今生(gns3改名)
下一篇:说说“用户无线网络时不时断开重连”的故障!(无线网络连接断断续续)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~