加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式学习笔记(一)──转义、替换、匹配

发布时间:2020-12-14 01:11:14 所属栏目:百科 来源:网络整理
导读:前言 最近项目用到正则表达式比较多,领会了正则表达式的强大,可以拿来做很多事情。以前只知道拿它去判断一个字符串是否符合一个模式,或者拿来在文本中查找字符串。其实,还可以用它编辑处理文本。事实上,查找依托于匹配,编辑依托于查找,根基还是匹配。

前言

最近项目用到正则表达式比较多,领会了正则表达式的强大,可以拿来做很多事情。以前只知道拿它去判断一个字符串是否符合一个模式,或者拿来在文本中查找字符串。其实,还可以用它编辑处理文本。事实上,查找依托于匹配,编辑依托于查找,根基还是匹配。

转义

正则表达式有独特的语法,有元字符。比如.会匹配任意字符(除去换行符n)。如果我们想.只匹配.,那么我们就需要转义。

问题来了,如果我们这样定义String line = ". hello world";,会出现非法转义符错误。因为java无法转义.。所以我们需要这样定义String line = ". hello world";,这样java就不会去转义.了,因为第二个被第一个转义了,它不再是转义符了。

在Python里面可以这么定义'''. hello world''',Python可以定义raw string,raw string中的不是转义字符了。

替换文本一——用固定文本替换

项目中有个需求,要把{0} variable {1} should begin with {2}bala variable bobo should begin with lala匹配。我的做法是将{0} variable {1} should begin with {2}转换为.* variable .* should begin with .*,然后用.* variable .* should begin with .*去匹配bala variable bobo should begin with lala

{0} variable {1} should begin with {2}转换为.* variable .* should begin with .*就需要用到正则表达式的编辑文本功能,先匹配{0}``{1}``{2}``{3}等等,然后将它们全部替换为·*。(注意,{``}也是元字符需要转义,所以正则表达式应该是{d+},转换到java string就是{d+})

//java
String patternString = "{d+}";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(resultString);
resultString =  matcher.replaceAll(".*");
//python
import re
pattern = re.sub('''{d+}''','''.*''',string)

替换文本二——用不固定文本替换

在项目中,正则表达式是要自动生成的。比如要把You should modify visibility of class or methods using getDeclaredConstructors(),getDeclaredConstructor(Class[]),setAccessible() or PrivilegedAction.转化为正则表达式。java里面,我们需要把(替换为()替换为)[替换为[]替换为].替换为.,否则生成的正则表达式会有语法错误。(它们在正则表达式中都有特殊使用,也有固定使用格式,所以需要转义。)

你可以用n个replaceAll去实现,而使用正则表达式则就优雅的多。

//java
    public static void main(String... args) {
        String input = "You should modify visibility of class or methods using getDeclaredConstructors(),getDeclaredConstructor(Class[]),setAccessible() or PrivilegedAction.";
        Pattern p = Pattern.compile("(|)|[|]|.");
        Matcher m = p.matcher(input);
        StringBuffer sb = new StringBuffer();
        while (m.find()) {
            String rep = "" + m.group(0);
            m.appendReplacement(sb,rep);
        }
        m.appendTail(sb);
        System.out.println(sb);
    }
    //You should modify visibility of class or methods using getDeclaredConstructors(),getDeclaredConstructor(Class[]),setAccessible() or PrivilegedAction.
//python
import re
pattern = re.sub('''[[].]''',lambda m: '''%s''' % m.group(0),input)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读