TychoTheTaco TychoTheTaco - 6 months ago 15
Android Question

Highlight IP address in string

Suppose I have the following string

User 192.168.46.3 connected to this AP
. I want to replace the IP address in there with
<font color='red'>"+192.168.46.3+"</font>
so that I can change it's color. What is the best way to achieve this?

Answer

This is the full code based on TimĀ“s answer:

String input = "User 192.168.46.3 connected to this AP";
String regex = "([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})";
String output = input.replaceAll(regex, "<font color='red'>$0</font>");
System.out.println(output);
Comments