Replace HTML codes with equivalent characters in Java [duplicate]

Also, is there any way to optimize this regex? Yes, don’t use regex for this task, use Apache StringEscapeUtils from Apache commons lang: import org.apache.commons.lang.StringEscapeUtils; … String withCharacters = StringEscapeUtils.unescapeHtml(yourString); JavaDoc says: Unescapes a string containing entity escapes to a string containing the actual Unicode characters corresponding to the escapes. Supports HTML 4.0 entities. For … Read more

java regex pattern unclosed character class

Assuming that you want to match \ and – and not ]: Pattern pattern = Pattern.compile(“^[a-zA-Z\300-\3770-9\u0153\346 \u002F.’\\\\-]*$”); You need to double escape your backslashes, as \ is also an escape character in regex. Thus \\] escapes the backslash for java but not for regex. You need to add another java-escaped \ in order to regex-escape … Read more

Convert Javascript regular expression to Java syntax

Change the leading and trailing “https://stackoverflow.com/” characters to ‘”‘, and then replace each ‘\’ with “\\”. Unlike, JavaScript, Perl and other scripting languages, Java doesn’t have a special syntax for regexes. Instead, they are (typically) expressed using Java string literals. But ‘\’ is the escape character in a Java string literal, so each ‘\’ in … Read more

What’s the difference between Mockito Matchers isA, any, eq, and same?

any() checks absolutely nothing. Since Mockito 2.0, any(T.class) shares isA semantics to mean “any T” or properly “any instance of type T“. This is a change compared to Mockito 1.x, where any(T.class) checked absolutely nothing but saved a cast prior to Java 8: “Any kind object, not necessary of the given class. The class argument … Read more

How to extract parameters from a given url

It doesn’t have to be regex. Since I think there’s no standard method to handle this thing, I’m using something that I copied from somewhere (and perhaps modified a bit): public static Map<String, List<String>> getQueryParams(String url) { try { Map<String, List<String>> params = new HashMap<String, List<String>>(); String[] urlParts = url.split(“\\?”); if (urlParts.length > 1) { … Read more