user3256396
user3256396

Reputation: 73

detect Hebrew letters in strings

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?

Upvotes: 5

Views: 4750

Answers (4)

Israel Shapira
Israel Shapira

Reputation: 71

To test that the String str contains Hebrew letters use:

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

returns true if str contains Hebrew letters.

Upvotes: 7

Yuriy N.
Yuriy N.

Reputation: 6107

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.

Upvotes: 2

npinti
npinti

Reputation: 52185

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.

Upvotes: 2

Scorcher
Scorcher

Reputation: 392

Pattern p = Pattern.compile("\\p{InHebrew}");
Matcher m = p.matcher(input);

Upvotes: 3

Related Questions