Java使用正则表达式实现字符串的替换,输出

Cheryl

在写程序的过程中遇到需要转码的问题,使用正则表达式使之正常化,如下


public class Test {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String input = "#\\u5173\\u95ed\\u7f13\\u5b58, \\u5373\\u65f6\\u5237\\u65b0\r\n" + 
                "#spring.freemarker.cache=false\r\n" + 
                "#spring.thymeleaf.cache=true\r\n" + 
                "\r\n" + 
                "#\\u70ed\\u90e8\\u7f72\\u751f\\u6548\r\n" + 
                "spring.devtools.restart.enabled=true\r\n" + 
                "#\\u8bbe\\u7f6e\\u91cd\\u542f\\u7684\\u76ee\\u5f55,\\u6dfb\\u52a0\\u90a3\\u4e2a\\u76ee\\u5f55\\u7684\\u6587\\u4ef6\\u9700\\u8981restart\r\n" + 
                "spring.devtools.restart.additional-paths=src/main/java\r\n" + 
                "# \\u4e3amybatis\\u8bbe\\u7f6e\\uff0c\\u751f\\u4ea7\\u73af\\u5883\\u53ef\\u5220\\u9664\r\n" + 
                "restart.include.mapper=/mapper-[\\\\w-\\\\.]+jar\r\n" + 
                "restart.include.pagehelper=/pagehelper-[\\\\w-\\\\.]+jar\r\n" + 
                "#\\u6392\\u9664\\u90a3\\u4e2a\\u76ee\\u5f55\\u7684\\u6587\\u4ef6\\u4e0d\\u9700\\u8981restart\r\n" + 
                "#spring.devtools.restart.exclude=static/**,public/**\r\n" + 
                "#classpath\\u76ee\\u5f55\\u4e0b\\u7684WEB-INF\\u6587\\u4ef6\\u5939\\u5185\\u5bb9\\u4fee\\u6539\\u4e0d\\u91cd\\u542f\r\n" + 
                "#spring.devtools.restart.exclude=WEB-INF/**\r\n" + 
                "\r\n" + 
                "############################################################\r\n" + 
                "#\r\n" + 
                "# REDIS \\u914d\\u7f6e\r\n" + 
                "#\r\n" + 
                "############################################################\r\n" + 
                "# Redis\\u6570\\u636e\\u5e93\\u7d22\\u5f15\\uff08\\u9ed8\\u8ba4\\u4e3a0\\uff09\r\n" + 
                "spring.redis.database=1\r\n" + 
                "# Redis\\u670d\\u52a1\\u5668\\u5730\\u5740\r\n" + 
                "spring.redis.host=192.168.1.191\r\n" + 
                "# Redis\\u670d\\u52a1\\u5668\\u8fde\\u63a5\\u7aef\\u53e3\r\n" + 
                "spring.redis.port=6379\r\n" + 
                "# Redis\\u670d\\u52a1\\u5668\\u8fde\\u63a5\\u5bc6\\u7801\\uff08\\u9ed8\\u8ba4\\u4e3a\\u7a7a\\uff09\r\n" + 
                "spring.redis.password=\r\n" + 
                "# \\u8fde\\u63a5\\u6c60\\u6700\\u5927\\u8fde\\u63a5\\u6570\\uff08\\u4f7f\\u7528\\u8d1f\\u503c\\u8868\\u793a\\u6ca1\\u6709\\u9650\\u5236\\uff09\r\n" + 
                "spring.redis.pool.max-active=1000\r\n" + 
                "# \\u8fde\\u63a5\\u6c60\\u6700\\u5927\\u963b\\u585e\\u7b49\\u5f85\\u65f6\\u95f4\\uff08\\u4f7f\\u7528\\u8d1f\\u503c\\u8868\\u793a\\u6ca1\\u6709\\u9650\\u5236\\uff09\r\n" + 
                "spring.redis.pool.max-wait=-1\r\n" + 
                "# \\u8fde\\u63a5\\u6c60\\u4e2d\\u7684\\u6700\\u5927\\u7a7a\\u95f2\\u8fde\\u63a5\r\n" + 
                "spring.redis.pool.max-idle=10\r\n" + 
                "# \\u8fde\\u63a5\\u6c60\\u4e2d\\u7684\\u6700\\u5c0f\\u7a7a\\u95f2\\u8fde\\u63a5\r\n" + 
                "spring.redis.pool.min-idle=2\r\n" + 
                "# \\u8fde\\u63a5\\u8d85\\u65f6\\u65f6\\u95f4\\uff08\\u6beb\\u79d2\\uff09\r\n" + 
                "spring.redis.timeout=0\r\n" + 
                "\r\n" + 
                "\r\n" + 
                "\r\n" + 
                "############################################################\r\n" + 
                "#\r\n" + 
                "# \\u914d\\u7f6e\\u6570\\u636e\\u6e90\\u76f8\\u5173    \\u4f7f\\u7528\\u963f\\u91cc\\u5df4\\u5df4\\u7684 druid \\u6570\\u636e\\u6e90\r\n" + 
                "#\r\n" + 
                "############################################################\r\n" + 
                "spring.datasource.url=jdbc:mysql://localhost:3306/leecx\r\n" + 
                "spring.datasource.username=root\r\n" + 
                "spring.datasource.password=root\r\n" + 
                "spring.datasource.driver-class-name=com.mysql.jdbc.Driver\r\n" + 
                "spring.datasource.druid.initial-size=1\r\n" + 
                "spring.datasource.druid.min-idle=1\r\n" + 
                "spring.datasource.druid.max-active=20\r\n" + 
                "spring.datasource.druid.test-on-borrow=true\r\n" + 
                "spring.datasource.druid.stat-view-servlet.allow=true\r\n" + 
                "\r\n" + 
                "\r\n" + 
                "############################################################\r\n" + 
                "#\r\n" + 
                "# mybatis \\u914d\\u7f6e\r\n" + 
                "#\r\n" + 
                "############################################################\r\n" + 
                "# mybatis \\u914d\\u7f6e\r\n" + 
                "mybatis.type-aliases-package=com.imooc.pojo\r\n" + 
                "mybatis.mapper-locations=classpath:mapper/*.xml\r\n" + 
                "# \\u901a\\u7528 Mapper \\u914d\\u7f6e\r\n" + 
                "mapper.mappers=com.imooc.utils.MyMapper\r\n" + 
                "mapper.not-empty=false\r\n" + 
                "mapper.identity=MYSQL\r\n" + 
                "# \\u5206\\u9875\\u63d2\\u4ef6\\u914d\\u7f6e\r\n" + 
                "pagehelper.helperDialect=mysql\r\n" + 
                "pagehelper.reasonable=true\r\n" + 
                "pagehelper.supportMethodsArguments=true\r\n" + 
                "pagehelper.params=count=countSql\r\n" + 
                "\r\n" + 
                "\r\n" + 
                "############################################################\r\n" + 
                "#\r\n" + 
                "# \\u914d\\u7f6ei18n \\u8d44\\u6e90\\u6587\\u4ef6\\uff0c\\u4f9bthymeleaf \\u8bfb\\u53d6\r\n" + 
                "#\r\n" + 
                "############################################################\r\n" + 
                "spring.messages.basename=i18n/messages\r\n" + 
                "spring.messages.cache-seconds=3600\r\n" + 
                "spring.messages.encoding=UTF-8\r\n" + 
                "\r\n" + 
                "\r\n" + 
                "#\\u8bbe\\u5b9a\\u9759\\u6001\\u6587\\u4ef6\\u8def\\u5f84\\uff0cjs,css\\u7b49\r\n" + 
                "spring.mvc.static-path-pattern=/static/**\r\n" + 
                "\r\n" + 
                "############################################################\r\n" + 
                "#\r\n" + 
                "# freemarker \\u9759\\u6001\\u8d44\\u6e90\\u914d\\u7f6e\r\n" + 
                "#\r\n" + 
                "############################################################\r\n" + 
                "#\\u8bbe\\u5b9aftl\\u6587\\u4ef6\\u8def\\u5f84\r\n" + 
                "spring.freemarker.template-loader-path=classpath:/templates\r\n" + 
                "# \\u5173\\u95ed\\u7f13\\u5b58, \\u5373\\u65f6\\u5237\\u65b0, \\u4e0a\\u7ebf\\u751f\\u4ea7\\u73af\\u5883\\u9700\\u8981\\u6539\\u4e3atrue\r\n" + 
                "spring.freemarker.cache=false\r\n" + 
                "spring.freemarker.charset=UTF-8\r\n" + 
                "spring.freemarker.check-template-location=true\r\n" + 
                "spring.freemarker.content-type=text/html\r\n" + 
                "spring.freemarker.expose-request-attributes=true\r\n" + 
                "spring.freemarker.expose-session-attributes=true\r\n" + 
                "spring.freemarker.request-context-attribute=request\r\n" + 
                "spring.freemarker.suffix=.ftl\r\n" + 
                "\r\n" + 
                "\r\n" + 
                "############################################################\r\n" + 
                "#\r\n" + 
                "# thymeleaf \\u9759\\u6001\\u8d44\\u6e90\\u914d\\u7f6e\r\n" + 
                "#\r\n" + 
                "############################################################\r\n" + 
                "spring.thymeleaf.prefix=classpath:/templates/\r\n" + 
                "spring.thymeleaf.suffix=.html\r\n" + 
                "spring.thymeleaf.mode=HTML5\r\n" + 
                "spring.thymeleaf.encoding=UTF-8\r\n" + 
                "spring.thymeleaf.content-type=text/html\r\n" + 
                "# \\u5173\\u95ed\\u7f13\\u5b58, \\u5373\\u65f6\\u5237\\u65b0, \\u4e0a\\u7ebf\\u751f\\u4ea7\\u73af\\u5883\\u9700\\u8981\\u6539\\u4e3atrue\r\n" + 
                "spring.thymeleaf.cache=false\r\n" + 
                "\r\n" + 
                "\r\n" + 
                "############################################################\r\n" + 
                "#\r\n" + 
                "# Server \\u670d\\u52a1\\u7aef\\u76f8\\u5173\\u914d\\u7f6e\r\n" + 
                "#\r\n" + 
                "############################################################\r\n" + 
                "# \\u914d\\u7f6eapi\\u7aef\\u53e3\\u53f7\r\n" + 
                "#server.port=8088\r\n" + 
                "# \\u914d\\u7f6econtext-path, \\u4e00\\u822c\\u6765\\u8bf4\\u8fd9\\u4e2a\\u914d\\u7f6e\\u5728\\u6b63\\u5f0f\\u53d1\\u5e03\\u7684\\u65f6\\u5019\\u4e0d\\u914d\\u7f6e\r\n" + 
                "#server.context-path=/IMooc\r\n" + 
                "# \\u9519\\u8bef\\u9875\\uff0c\\u6307\\u5b9a\\u53d1\\u751f\\u9519\\u8bef\\u65f6\\uff0c\\u8df3\\u8f6c\\u7684URL --> BasicErrorController\r\n" + 
                "#server.error.path=/error\r\n" + 
                "# session\\u6700\\u5927\\u8d85\\u65f6\\u65f6\\u95f4(\\u5206\\u949f)\\uff0c\\u9ed8\\u8ba4\\u4e3a30\\u5206\\u949f\r\n" + 
                "server.session-timeout=60\r\n" + 
                "# \\u8be5\\u670d\\u52a1\\u7ed1\\u5b9aIP\\u5730\\u5740\\uff0c\\u542f\\u52a8\\u670d\\u52a1\\u5668\\u65f6\\u5982\\u672c\\u673a\\u4e0d\\u662f\\u8be5IP\\u5730\\u5740\\u5219\\u629b\\u51fa\\u5f02\\u5e38\\u542f\\u52a8\\u5931\\u8d25, \r\n" + 
                "# \\u53ea\\u6709\\u7279\\u6b8a\\u9700\\u6c42\\u7684\\u60c5\\u51b5\\u4e0b\\u624d\\u914d\\u7f6e, \\u5177\\u4f53\\u6839\\u636e\\u5404\\u81ea\\u7684\\u4e1a\\u52a1\\u6765\\u8bbe\\u7f6e\r\n" + 
                "#server.address=192.168.1.2\r\n" + 
                "\r\n" + 
                "############################################################\r\n" + 
                "# Server - tomcat \\u76f8\\u5173\\u5e38\\u7528\\u914d\\u7f6e\r\n" + 
                "############################################################\r\n" + 
                "# tomcat\\u6700\\u5927\\u7ebf\\u7a0b\\u6570, \\u9ed8\\u8ba4\\u4e3a200\r\n" + 
                "#server.tomcat.max-threads=250\r\n" + 
                "# tomcat\\u7684URI\\u7f16\\u7801\r\n" + 
                "server.tomcat.uri-encoding=UTF-8\r\n" + 
                "# \\u5b58\\u653eTomcat\\u7684\\u65e5\\u5fd7\\u3001Dump\\u7b49\\u6587\\u4ef6\\u7684\\u4e34\\u65f6\\u6587\\u4ef6\\u5939\\uff0c\\u9ed8\\u8ba4\\u4e3a\\u7cfb\\u7edf\\u7684tmp\\u6587\\u4ef6\\u5939\r\n" + 
                "#\\uff08\\u5982\\uff1aC:\\Users\\Shanhy\\AppData\\Local\\Temp\\uff09\r\n" + 
                "#server.tomcat.basedir=H:/springboot-tomcat-tmp\r\n" + 
                "# \\u6253\\u5f00Tomcat\\u7684Access\\u65e5\\u5fd7\\uff0c\\u5e76\\u53ef\\u4ee5\\u8bbe\\u7f6e\\u65e5\\u5fd7\\u683c\\u5f0f\\u7684\\u65b9\\u6cd5\\uff1a\r\n" + 
                "#server.tomcat.access-log-enabled=true\r\n" + 
                "#server.tomcat.access-log-pattern=\r\n" + 
                "# accesslog\\u76ee\\u5f55\\uff0c\\u9ed8\\u8ba4\\u5728basedir/logs\r\n" + 
                "#server.tomcat.accesslog.directory=\r\n" + 
                "# \\u65e5\\u5fd7\\u6587\\u4ef6\\u76ee\\u5f55\r\n" + 
                "#logging.path=H:/springboot-tomcat-tmp\r\n" + 
                "# \\u65e5\\u5fd7\\u6587\\u4ef6\\u540d\\u79f0\\uff0c\\u9ed8\\u8ba4\\u4e3aspring.log\r\n" + 
                "#logging.file=myapp.log";
        
        String pattern = "\\\\u[0-9a-fA-F]{4}";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(input);
        
        while(m.find()) {
            String target = m.toString().substring(m.toString().lastIndexOf("\\")+2, m.toString().lastIndexOf("]"));
            char c = (char)Integer.parseInt(target, 16);
            input = input.replaceFirst(pattern, String.valueOf(c));
        }
        System.out.println(input);
    }
}

输出结果为:

#关闭缓存, 即时刷新
#spring.freemarker.cache=false
#spring.thymeleaf.cache=true

#热部署生效
spring.devtools.restart.enabled=true
#设置重启的目录,添加那个目录的文件需要restart
spring.devtools.restart.additional-paths=src/main/java
# 为mybatis设置,生产环境可删除
restart.include.mapper=/mapper-[\\w-\\.]+jar
restart.include.pagehelper=/pagehelper-[\\w-\\.]+jar
#排除那个目录的文件不需要restart
#spring.devtools.restart.exclude=static/**,public/**
#classpath目录下的WEB-INF文件夹内容修改不重启
#spring.devtools.restart.exclude=WEB-INF/**

############################################################
#
# REDIS 配置
#
############################################################
# Redis数据库索引(默认为0)
spring.redis.database=1
# Redis服务器地址
spring.redis.host=192.168.1.191
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=1000
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=10
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=2
# 连接超时时间(毫秒)
spring.redis.timeout=0



############################################################
#
# 配置数据源相关    使用阿里巴巴的 druid 数据源
#
############################################################
spring.datasource.url=jdbc:mysql://localhost:3306/leecx
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.initial-size=1
spring.datasource.druid.min-idle=1
spring.datasource.druid.max-active=20
spring.datasource.druid.test-on-borrow=true
spring.datasource.druid.stat-view-servlet.allow=true


############################################################
#
# mybatis 配置
#
############################################################
# mybatis 配置
mybatis.type-aliases-package=com.imooc.pojo
mybatis.mapper-locations=classpath:mapper/*.xml
# 通用 Mapper 配置
mapper.mappers=com.imooc.utils.MyMapper
mapper.not-empty=false
mapper.identity=MYSQL
# 分页插件配置
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql


############################################################
#
# 配置i18n 资源文件,供thymeleaf 读取
#
############################################################
spring.messages.basename=i18n/messages
spring.messages.cache-seconds=3600
spring.messages.encoding=UTF-8


#设定静态文件路径,js,css等
spring.mvc.static-path-pattern=/static/**

############################################################
#
# freemarker 静态资源配置
#
############################################################
#设定ftl文件路径
spring.freemarker.template-loader-path=classpath:/templates
# 关闭缓存, 即时刷新, 上线生产环境需要改为true
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.request-context-attribute=request
spring.freemarker.suffix=.ftl


############################################################
#
# thymeleaf 静态资源配置
#
############################################################
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
# 关闭缓存, 即时刷新, 上线生产环境需要改为true
spring.thymeleaf.cache=false


############################################################
#
# Server 服务端相关配置
#
############################################################
# 配置api端口号
#server.port=8088
# 配置context-path, 一般来说这个配置在正式发布的时候不配置
#server.context-path=/IMooc
# 错误页,指定发生错误时,跳转的URL --> BasicErrorController
#server.error.path=/error
# session最大超时时间(分钟),默认为30分钟
server.session-timeout=60
# 该服务绑定IP地址,启动服务器时如本机不是该IP地址则抛出异常启动失败, 
# 只有特殊需求的情况下才配置, 具体根据各自的业务来设置
#server.address=192.168.1.2

############################################################
# Server - tomcat 相关常用配置
############################################################
# tomcat最大线程数, 默认为200
#server.tomcat.max-threads=250
# tomcat的URI编码
server.tomcat.uri-encoding=UTF-8
# 存放Tomcat的日志、Dump等文件的临时文件夹,默认为系统的tmp文件夹
#(如:C:\Users\Shanhy\AppData\Local\Temp)
#server.tomcat.basedir=H:/springboot-tomcat-tmp
# 打开Tomcat的Access日志,并可以设置日志格式的方法:
#server.tomcat.access-log-enabled=true
#server.tomcat.access-log-pattern=
# accesslog目录,默认在basedir/logs
#server.tomcat.accesslog.directory=
# 日志文件目录
#logging.path=H:/springboot-tomcat-tmp
# 日志文件名称,默认为spring.log
#logging.file=myapp.log
阅读 1.6k

Java技术栈 [链接]

13 声望
0 粉丝
0 条评论

Java技术栈 [链接]

13 声望
0 粉丝
文章目录
宣传栏