微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

前言

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

转义

正则表达式有独特的语法,有元字符。比如.会匹配任意字符(除去换行符\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)

原文地址:https://www.jb51.cc/regex/360158.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐