0

I'm using regex to match if a string contains at least an alphabet in it. but when the string contains special characters (not in regular Cp1252 encoding) it is giving me result as not matched even when string is having other alphabets. Here is my code

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringContainsAlphabetsExample {
public static void main(String[] args) {

Pattern pattern = Pattern.compile(".*[a-zA-Z]+.*");

 String splChar = "has Alphabet";
 Matcher matcher = pattern.matcher(splChar);

 if (matcher.matches()) {
      System.out.println("Matched");
 } else {
      System.out.println("Not Matched");
  }
 }
}

Here is the String that causing error

String splChar = "This text has a special char.​
";

Copy and paste the above string in notepad to see the special char.

Please let me know how to filter this special characters in the string or any work around to find if string contains alphabets.

  • 1
    Regexes don't have notions of character sets. You're better off building a String that's the original stripped of all non-cp1252 characters, and then apply your regex on it. – kumesana Jul 06 '18 at 13:46
  • @WiktorStribiżew he literally told you to copy/paste the text in a text editor. Maybe you should? – kumesana Jul 06 '18 at 13:49
  • 1
    There are 2 trailing chars there,`\u200B\u2028`. `\u2028` is the issue, it is a line break and `.` does not match line breaks. Add `(?s)` at the pattern start. Use `[A-Za-z]` with `matcher.find()` rather than `"(?s).*[a-zA-Z]+.*"` with `matcher.matches()`. `Pattern.compile("[a-zA-Z]");` and then `if (matcher.find()) {`. – Wiktor Stribiżew Jul 06 '18 at 13:52
  • @WiktorStribiżew 'kay then, sorry your stack doesn't see it. There is a special char. – kumesana Jul 06 '18 at 13:52
  • Thanks @WiktorStribiżew suggested solution works. – Yuvaraj Chitela Jul 06 '18 at 14:14

0 Answers0