检查 String 是否只包含拉丁字符? [英] Check String whether it contains only Latin characters?

查看:52
本文介绍了检查 String 是否只包含拉丁字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我正在开发 GWT 应用程序,用户可以在其中用日语输入他的详细信息.但是'userid' 和'password' 应该只包含英文字符(拉丁字母).如何为此验证字符串?

I am developing GWT application where user can enter his details in Japanese. But the 'userid' and 'password' should only contain English characters(Latin Alphabet). How to validate Strings for this?

推荐答案

您可以使用 String#matches() 有点regex 为此.w 覆盖了拉丁字符.

You can use String#matches() with a bit regex for this. Latin characters are covered by w.

所以应该这样做:

boolean valid = input.matches("\w+");

顺便说一下,这也包括数字和下划线 _.不确定这是否有害.否则你可以使用 [A-Za-z]+ 代替.

This by the way also covers numbers and the underscore _. Not sure if that harms. Else you can just use [A-Za-z]+ instead.

如果您还想涵盖变音字符(ä、é、ò 等上,这些是每个定义也是拉丁字符),那么您需要先规范化它们并在匹配之前去掉变音符号,仅仅是因为没有 (已记录) 正则表达式涵盖变音符号.

If you want to cover diacritical characters as well (ä, é, ò, and so on, those are per definition also Latin characters), then you need to normalize them first and get rid of the diacritical marks before matching, simply because there's no (documented) regex which covers diacriticals.

String clean = Normalizer.normalize(input, Form.NFD).replaceAll("\p{InCombiningDiacriticalMarks}+", "");
boolean valid = clean.matches("\w+");

更新:Java 中有一个未公开的正则表达式,它也涵盖了变音符号,p{L}.

Update: there's an undocumented regex in Java which covers diacriticals as well, the p{L}.

boolean valid = input.matches("\p{L}+");

以上适用于 Java 1.6.

Above works at Java 1.6.

这篇关于检查 String 是否只包含拉丁字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆