Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

regular expression.  RSS feed

 
Rajeev Ravindran
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have the following util method that make use of Regular Expression to check if the name entered by the user is valid or not. Its a java method


private static boolean isValidName(String name) {

boolean valid = false;
// Check for correct phone number
if (name.matches("[a-zA-Z\\x20\\x27\\x5F\\x2D]*") ) {

valid = true;
}

return valid;
}


It works good for english characters. But if i enter a Frech name with accents like Mich�le its not working. could someone help me with this ? im not really good in regular exprssion. Any help on this is really appretiated. Sorry if im posting the question in a wrong forum. its more about regular experession so i thought to post it here.

Thanks,
Rajeev
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not a Java Forum...moving to JIG
 
Tim LeMaster
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your regular expression is allowing a-z A-Z space ' - _

� is not in that list. You could add each additional character you wanted to support (\u00E0) will add �. Or figure out a range of unicode characters to support.

The question is more about unicode than regular expressions.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably the easiest thing to do here is use the Unicode category \p{L} to test for all possible Unicode letters:

If you want to restrict the list to only letters in, say, the Basic Latin and Latin-1 blocks (which should cover most western European languages at least) then you could use something like:
 
Rajeev Ravindran
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim , Jum, Thanks for your reply !!

I fixed by adding all possible characters which are acceptable to the regular expression


if (name.matches("[��������������������������������a-zA-Z\\x20\\x27\\x5F\\x2D]*") ) {
valid = true;
}


Thanks again,
Rajeev !
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!