2

I have to replace "font-size" value in my HTML text with my required text size for my android app.

I am getting

font-size: 26.2101px;

font-size: 0.9em

font-size: 31px;

I want to replace "font-size" attribute with value "16px". How can I achieve this?

4 Answers 4

2

You should be able to achieve this with a regular expression.

Something in the form of font-size: [0-9].?[0-9](px|em)

For more details on regular expressions you can check out this page:

To Test this expression, you can go here

Sign up to request clarification or add additional context in comments.

Comments

2

You can use Regular Expression to replace your text as

String htmlString = Html.fromHtml("yourHtmlString").toString();
String regex = "font-size: \\(\\[0-9\\]*\\(.*\\)\\[0-9\\]+px\\);";
htmlString.replaceAll(regex, "font-size: 30px");

Hope this helps.

Comments

1

This is possible using RegEx

(font-size: \\d+.?\\d+((em)?(px)?))

Example

     String s="font-size: 26.2101px;";
     String regEx="(font-size: \\d+.?\\d+((em)?(px)?))"; 
     s=s.replaceAll(regEx,"16px");

2 Comments

Thanks @NJ its working, some change in your code for better output. String regEx="(font-size: \\d+.?\\d+((em)?(px)?))";
Welcome ,Glad to help you, please do vote up and accept
1

If I understood your problem correctly then you are probably looking for something like this:

public static void main (String[] args)
{
    /* Text To-Be Replaced */
    String myTextString = "font-size: 26.2101px;\nfont-size: 0.9em\nfont-size: 31px;";
    /* Regex Replace */
    System.out.println(myTextString.replaceAll("font-size: [0-9]+px;","font-size: 16px;");
}

Output:

font-size: 26.2101px;
font-size: 0.9em
font-size: 16px;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.