gpt4 book ai didi

java - Android 替换为正则表达式

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:47:32 24 4
gpt4 key购买 nike

我在 Android 中有一个字符串。我想用一些 html 包装 4 个或更多连续数字的所有实例。我想这将通过正则表达式来完成,但我什至很难让最基本的正则表达式发挥作用。

有人可以帮我解决这个问题吗?

我想改变:

var input = "My phone is 1234567890 and my office is 7894561230";

var output = "My phone is <u>1234567890</u> and my office is <u>7894561230</u>";

最佳答案

这样做就可以了:

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);

关于java - Android 替换为正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12377838/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com