Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android replace with regex

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>";
like image 795
raydowe Avatar asked Sep 11 '12 21:09

raydowe


People also ask

Can I use regex in replace?

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.

How does regex replace work?

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.

What is regex Android?

Regular Expression basically defines a search pattern, pattern matching, or string matching. It is present in java. util. regex package.

How do you change special characters on Android?

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 "[;\\/:*?


1 Answers

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);
like image 139
Keppil Avatar answered Oct 19 '22 01:10

Keppil