Java String match html character reference

Description

Java String match html character reference

//package com.demo2s;

import java.util.regex.Pattern;

public class Main {
    public static void main(String[] argv) throws Exception {
        String s = "$#A1";
        System.out.println(containsCharRef(s));
    }//from ww w  .ja va  2 s  . c  o  m

    private static final Pattern characterReferencePattern = Pattern.compile("&#?[a-zA-Z0-9]{1,8};");

    /**
     * Determines if a string contains what looks like an html character
     * reference. Useful for deciding whether unescaping is necessary.
     */
    public static boolean containsCharRef(String s) {
        return characterReferencePattern.matcher(s).find();
    }
}



PreviousNext

Related