Java 用regex替换Android
我在Android中有一个字符串。我想用一些html包装4个或更多连续数字的所有实例。我想这将通过正则表达式完成,但我很难让最基本的正则表达式工作 有人能帮我吗 我想改变一下:Java 用regex替换Android,java,android,regex,Java,Android,Regex,我在Android中有一个字符串。我想用一些html包装4个或更多连续数字的所有实例。我想这将通过正则表达式完成,但我很难让最基本的正则表达式工作 有人能帮我吗 我想改变一下: var input = "My phone is 1234567890 and my office is 7894561230"; 到 var output=“我的电话是1234567890,我的办公室是7894561230”; 这样就可以了: String input = "My phone is 123456789
var input = "My phone is 1234567890 and my office is 7894561230";
到
var output=“我的电话是1234567890,我的办公室是7894561230”;
这样就可以了:
String input = "My phone is 1234567890 and my office is 7894561230";
String regex = "\\d{4,}";
String output = input.replaceAll(regex, "<u>$0</u>");
System.out.println(output);
String input=“我的电话是1234567890,我的办公室是7894561230”;
字符串regex=“\\d{4,}”;
字符串输出=input.replaceAll(regex,“$0”);
系统输出打印项次(输出);
太棒了,我不知道你可以将$#与String.replaceAll一起使用。这让生活变得更轻松。谢谢。@Keppil,这是什么“\\d{4,}”。我想替换一个@RahmathullahMPulikkal:\\d{4,}
表示4个或更多的数字。有很多关于如何搜索和替换字符串的帖子,你应该能够很快找到一个来帮助你。
String input = "My phone is 1234567890 and my office is 7894561230";
String regex = "\\d{4,}";
String output = input.replaceAll(regex, "<u>$0</u>");
System.out.println(output);