Kat
Kat

Reputation: 2500

How do I expand this replace expression to add weird alpha characters like "ü"?

I have this replacement expression here:

 String firstName = mFirstName.getText().toString().trim().replace(" ", "");
 String lastName = mLastName.getText().toString().trim().replace(" ", "");

 firstName = firstName.replaceAll("[^A-Za-z'-]", "");
 lastName = lastName.replaceAll("[^A-Za-z'-]", "");

It works really well and quickly. However it doesn't allow for the international ascii characters 128-165, say like umlauts. But I don't want the characters after that "()|-" in the string to be included. Is there a way to include that all in one replace all, or do I have to separate it out into multiple expressions?

Here's what I've tried (unsuccessfully) :

    firstName = firstName.replaceAll("[^A-Za-zÀ-Ÿ'-]", "");
    lastName = lastName.replaceAll("[^Alpha'-]", "");

It still replaces the characters.

Upvotes: 0

Views: 63

Answers (2)

kaykay
kaykay

Reputation: 556

[^A-Za-z\\x80-\\xa5'-] will additionally match characters with ASCII codes 128-165 (80 - A5 in hex)

Upvotes: 1

Akash Thakare
Akash Thakare

Reputation: 23002

You can use \p{L} which matches any Unicode character which is letter.

String strUmlaut = "ÀèŸ";
System.out.println(strUmlaut.matches("\\p{L}+"));

OUTPUT

true

Upvotes: 3

Related Questions