5

I am trying to write some code that changes files with hebrew letters in them to valid english names insted, but i am having problems understading how to detect those files, i built a filter for listfiles function.

Also i have searched online and i couldn't find an answer but this one:

How to tell if a string contains characters in Hebrew using PHP?

but its not java, its php. any ideas?

Community
  • 1
  • 1
user3256396
  • 73
  • 1
  • 3
  • Maybe this could help : http://stackoverflow.com/questions/8987119/how-to-capture-hebrew-with-regex-in-java – K.C. Jan 31 '14 at 08:20

4 Answers4

7

To test that the String str contains Hebrew letters use:

str.matches (".*[א-ת]+.*")

returns true if str contains Hebrew letters.

2
Pattern p = Pattern.compile("\\p{InHebrew}");
Matcher m = p.matcher(input);
Scorcher
  • 382
  • 2
  • 9
2

According to this page, there is a regex category for unicode Hebrew literals. This regex: \\p{Hebrew} should yield true should a string contain a Hebrew literal.

npinti
  • 51,780
  • 5
  • 72
  • 96
2

Chosen answer is not working in my case with mixed English and Hebrew string.

    String fileName = "ףךלחףךלחץ.msg";
    Pattern p = Pattern.compile("\\p{InHebrew}", Pattern.UNICODE_CASE);

    System.out.println(p.matcher(fileName).matches());  //false

Output: false.

To check if string contains some Hebrew letters next code used:

    String fileName = "ףךלחףךלחץ.msg";
    Pattern p = Pattern.compile("\\p{InHebrew}", Pattern.UNICODE_CASE);
    Matcher m = null;

    boolean hebrewDetected = false;
    for (int i = 0; i < fileName.length() && !hebrewDetected; i++){
        String letter = fileName.charAt(i) + "";
        m = p.matcher(letter);
        hebrewDetected = m.matches();
        if (hebrewDetected){
             break;
        }
    }

    System.out.println("hebrewDetected=" + hebrewDetected ); //true

Output : true.

Yuriy N.
  • 4,936
  • 2
  • 38
  • 31