简体   繁体   English

正则表达式查找不区分大小写的用户名

[英]Regex to find a username case insensitive

I am trying to create a method to check for unique usernames. 我正在尝试创建一种方法来检查唯一的用户名。 For example, if the username JOHN exists, someone else shouldn't pick the username John . 例如,如果用户名JOHN存在,则其他人不应选择用户名John My only problem is that I can't find the good regex for it. 我唯一的问题是我找不到适合它的正则表达式。 I tried the following: 我尝试了以下方法:

ParseQuery query = ParseUser.getQuery();
String regex = nickname.getText().toString().toLowerCase();
query.whereMatches("username", regex, "i");

But this also gives the users who have the username Johnny . 但是,这也会给拥有用户名Johnny的用户。 How can I only find the username John case insensitive? 我怎样才能只发现用户名John不区分大小写?

Replacing "username" with "^username$" should ensure that the entire string matches. "^username$"替换"username"应确保整个字符串匹配。 Since you are passing the i flag, it should match without taking into consideration the difference in casing. 由于您正在传递i标志,因此它应该匹配而不考虑大小写的差异。

That being said, I am not 100% sure if regular expressions are the answer, since all you need to do is case insensitive comparisons, which should be handled by some string function. 话虽这么说,我不确定100%是否可以使用正则表达式,因为您所要做的只是不区分大小写的比较,应该由某些字符串函数来处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM