java Android 用正则表达式替换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12377838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android replace with regex
提问by raydowe
I have a string in Android. I would like to wrap all the instances of 4 or more continuous digits with some html. I imagine this will be done with regex, but I have a hard time getting even the most basic regular expressions to work.
我在 Android 中有一个字符串。我想用一些 html 包装 4 个或更多连续数字的所有实例。我想这将通过正则表达式完成,但我很难让最基本的正则表达式工作。
Can someone help me with this?
有人可以帮我弄这个吗?
I would like to change:
我想改变:
var input = "My phone is 1234567890 and my office is 7894561230";
To
到
var output = "My phone is <u>1234567890</u> and my office is <u>7894561230</u>";
回答by Keppil
This will do it:
这将做到:
String input = "My phone is 1234567890 and my office is 7894561230";
String regex = "\d{4,}";
String output = input.replaceAll(regex, "<u>##代码##</u>");
System.out.println(output);