Java 正则表达式参数 [英] Java Regex Arguments

查看:33
本文介绍了Java 正则表达式参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以如果我想用给定的值替换所有数字,我可以使用

"hello8".replaceAll("[1-9]", "!");

<块引用>

你好!

现在有没有办法获取实际匹配的数字并将其添加到字符串中?

例如

<块引用>

你好!8

解决方案

一个选项是设置捕获组:

"hello8".replaceAll("([1-9])", "!$1");

另一种选择是使用$0,表示整个匹配:

"hello8".replaceAll("[1-9]", "!$0");

另见:regular-expressions.info/java

So if I wished to replace all numbers with a given value, I could just use

"hello8".replaceAll("[1-9]", "!");

hello!

Now is there a way to get the number that is actually being matched and add that to the string?

e.g.

hello!8

解决方案

One option is to set a capturing group:

"hello8".replaceAll("([1-9])", "!$1");

Another option is to use $0, which means the whole match:

"hello8".replaceAll("[1-9]", "!$0");

See also: regular-expressions.info/java

这篇关于Java 正则表达式参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆