java 如何使用java正则表达式验证字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26622157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to validate a string using java regex?
提问by KirbyAbadilla
I want to create a program that has the capability to check a string if it is valid to be as a person's name. But I am struggling to use regular expression for validating the string if its acceptable to be a person's name or not. Can you help me to implement the right conditions in my code? A string will be considered as a person's name if the following conditions are fulfilled:
我想创建一个程序,该程序能够检查字符串是否作为人名有效。但是我正在努力使用正则表达式来验证字符串是否可以接受为人名。你能帮我在我的代码中实现正确的条件吗?如果满足以下条件,字符串将被视为人名:
- no space before first word
- no non-word character
- no 2 or more consecutive spaces
- 第一个词前没有空格
- 没有非单词字符
- 没有 2 个或更多连续空格
I would also like to remove a space if it exists after the last word in my string. I am doing all of this just to force a user to input the right format of text that I will post soon on my JSON. That's why everything should be validated at the first place. No problem about whitespaces because I already defined the right inputType of my EditText on my XML file.
我还想删除一个空格,如果它存在于我的字符串中的最后一个单词之后。我做这一切只是为了强迫用户输入正确格式的文本,我将很快在我的 JSON 上发布这些文本。这就是为什么一切都应该首先被验证的原因。空格没有问题,因为我已经在 XML 文件中定义了 EditText 的正确 inputType。
This is the code that I tried to implement:
这是我试图实现的代码:
public boolean isFirstnameValid(String regex, String text){
Pattern checkRegex = Pattern.compile(regex);
Matcher regexMatcher = checkRegex.matcher(text);
while(regexMatcher.find()){
if(regexMatcher.group().length()!=0){
Log.e("searched",regexMatcher.group().trim());
}
}
return false;
// I returned false because, I'm still confused about what conditions should I implement.
}
This is the main method where my actual parameter is implemented:
这是实现我的实参的主要方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// String firstname = "Kirby Aster";
// String lastname = "Abadilla";
et =(EditText) findViewById (R.id.editText1);
b = (Button) findViewById (R.id.button1);
b.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String text = et.getText().toString();
isFirstnameValid("[A-Za-z]{1,}", text);
}
});
}
采纳答案by blackSmith
I don't like the implementation of isFirstnameValid
method. I think you are making it a bit complex than it should be. I would use simple String.matches
to do the job, eg :
我不喜欢isFirstnameValid
方法的实现。我认为你让它变得有点复杂。我会使用 simpleString.matches
来完成这项工作,例如:
public boolean isFirstnameValid(String text){
return text..matches("^([A-Za-z]+)(\s[A-Za-z]+)*\s?$");
}
The above regex meets all your conditions including allowing a space at the end. You may consider another condition of capital letter at the first of each word(regex will change a little). Then call it like :
上述正则表达式满足您的所有条件,包括在末尾允许一个空格。您可以考虑在每个单词的第一个大写字母的另一个条件(正则表达式会略有变化)。然后像这样调用它:
if( isFirstnameValid(text) ){
text = text.trim();
} else {
// define your failing condition here
}
If you have any query feel free to ask.
如果您有任何疑问,请随时询问。