原创

【python 正则表达式】正则表达式替换中括号内的内容

目标:替换中括号里面的内容包括中括号
如文本:aa[hong]tt
得到文本:aatt

# -*- coding:utf-8 -*-

import re
s='aa[hong]tt'
k=re.sub('\[[a-zA-Z0-9]+\]','',s)
print(k)

运行结果

aaa4444

Process finished with exit code 0

java版本:

public class javaregxp {

    public static void main(String[] args) {


        String content = "aa[hongdan]tt";
        String pattern = "\\[[a-zA-Z0-9\\]]+\\]";
        String content2 = content.replaceAll(pattern, "");
        System.out.println(content2);



    }
}

运行结果:

aatt

Process finished with exit code 0
正文到此结束