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.
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>";
The Regex. Replace(String, String, MatchEvaluator, RegexOptions) method is useful for replacing a regular expression match if any of the following conditions is true: If the replacement string cannot readily be specified by a regular expression replacement pattern.
The REGEXREPLACE( ) function uses a regular expression to find matching patterns in data, and replaces any matching values with a new string. standardizes spacing in character data by replacing one or more spaces between text characters with a single space.
Regular Expression basically defines a search pattern, pattern matching, or string matching. It is present in java. util. regex package.
static String replaceString(String string) { return string. replaceAll("[^A-Za-z0-9 ]","");// removing all special character. } this is work great but if user will enter the other language instead of eng then this code will replace the char of other language. so this "[;\\/:*?
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>$0</u>");
System.out.println(output);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With