简体   繁体   中英

Java regex to match 2 special charater followed by not known number of digits

Input may be like

Hi how are you $#85865865 ? what is the day there .

output :

Hi how are you ? what is the day there .

public class remochar {
    public static void main(String args[]) {
        String input = "Hi how are you ? hello  &#4567 ghsgsgsf ";
        String regx = "&#";

        char[] ca = regx.toCharArray();
        for (char c : ca) {
            input = input.replace("&#", "");
        }

        System.out.println(input);
    }
}

You are welcome:

\&#[0-9]+

You can use, for example, this online resource to test your regexes: https://regex101.com/ . It also explains how regexes work. In this case:

\\&#[0-9]+

"\\&" matches the character "$" literally (case sensitive)

"#" matches the character "#" literally (case sensitive)

Match a single character present in the list below [0-9]+

  • "+" Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy) 0-9 a single character in
  • the range between 0 (index 48) and 9 (index 57) (case sensitive)
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class remochar {
    public static void main(String args[]) {
        System.out.println("Sample Input and Output :");
        Scanner Iname = new Scanner(System.in);    

        String input = Iname.nextLine();
        String inp = input;

        Pattern pattern = Pattern.compile("&#+[0-9]");
        Matcher matcher = pattern.matcher(inp);
        if(matcher.find()) {
            System.out.println("yes");
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM