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


Posted in Python onSeptember 15, 2021

参考资料:

  1. 正则表达式语法?菜鸟教程
  2. 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 = " && "
    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*\((.+?)\)"
    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)

总结

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

Python 相关文章推荐
python网络编程之UDP通信实例(含服务器端、客户端、UDP广播例子)
Apr 25 Python
基于Django的python验证码(实例讲解)
Oct 23 Python
浅谈Python中的作用域规则和闭包
Mar 20 Python
用TensorFlow实现戴明回归算法的示例
May 02 Python
使用python画个小猪佩奇的示例代码
Jun 06 Python
python实现对指定字符串补足固定长度倍数截断输出的方法
Nov 15 Python
解决安装pycharm后不能执行python脚本的问题
Jan 19 Python
pytorch中的卷积和池化计算方式详解
Jan 03 Python
关于Pytorch的MLP模块实现方式
Jan 07 Python
基于python实现计算且附带进度条代码实例
Mar 31 Python
Django 后台带有字典的列表数据与页面js交互实例
Apr 03 Python
图神经网络GNN算法
May 11 Python
Python编程编写完善的命令行工具
Sep 15 #Python
python可视化之颜色映射详解
python的变量和简单数字类型详解
Sep 15 #Python
深入浅析Django MTV模式
python 进阶学习之python装饰器小结
Sep 04 #Python
自动在Windows中运行Python脚本并定时触发功能实现
Sep 04 #Python
关于python爬虫应用urllib库作用分析
You might like
香妃
2021/03/03 冲泡冲煮
简单了解将WordPress中的工具栏移到底部的小技巧
2015/12/31 PHP
Laravel框架实现的rbac权限管理操作示例
2019/01/16 PHP
PHP里的$_GET数组介绍
2019/03/22 PHP
浅析PHP中的 inet_pton 网络函数
2019/12/16 PHP
Node.js 学习笔记之简介、安装及配置
2015/03/03 Javascript
js实现仿网易点击弹出提示同时背景变暗效果
2015/08/13 Javascript
基于javascript简单实现对身份证校验
2021/01/25 Javascript
js获取iframe中的window对象的实现方法
2016/05/20 Javascript
ionic进入多级目录后隐藏底部导航栏(tabs)的完美解决方案
2016/11/23 Javascript
JS验证字符串功能
2017/02/22 Javascript
js 概率计算(简单版)
2017/09/12 Javascript
小试SVG之新手小白入门教程
2019/01/08 Javascript
vue项目首屏打开速度慢的解决方法
2019/03/31 Javascript
Python os模块介绍
2014/11/30 Python
Python简单检测文本类型的2种方法【基于文件头及cchardet库】
2016/09/18 Python
python编程培训 python培训靠谱吗
2018/01/17 Python
Sanic框架基于类的视图用法示例
2018/07/18 Python
python对列进行平移变换的方法(shift)
2019/01/10 Python
在Pycharm中修改文件默认打开方式的方法
2019/01/17 Python
python实现图片九宫格分割
2021/03/07 Python
python try except返回异常的信息字符串代码实例
2019/08/15 Python
tensorflow实现测试时读取任意指定的check point的网络参数
2020/01/21 Python
Python AutoCAD 系统设置的实现方法
2020/04/01 Python
使用matplotlib的pyplot模块绘图的实现示例
2020/07/12 Python
乐高积木玩具美国官网:LEGO Shop US
2016/09/16 全球购物
Origins悦木之源英国官网:雅诗兰黛集团高端植物护肤品牌
2017/11/06 全球购物
应届毕业生专业个人求职自荐信格式
2013/11/20 职场文书
行政助理的岗位职责
2014/02/18 职场文书
人力资源经理的岗位职责
2014/03/02 职场文书
党员一句话承诺大全
2014/03/28 职场文书
《放小鸟》教学反思
2014/04/20 职场文书
2014年电厂工作总结
2014/12/04 职场文书
Python爬虫基础之简单说一下scrapy的框架结构
2021/06/26 Python
业余无线电通联Q语
2022/02/18 无线电
mysql sql常用语句大全
2022/06/21 MySQL