正则表达式模式接受逗号或分号分隔的值 [英] Regex pattern accepting comma or semicolon separated values
本文介绍了正则表达式模式接受逗号或分号分隔的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一个正则表达式模式,该模式仅接受输入字段的逗号分隔值.
I need a regex pattern that accepts only comma separated values for an input field.
例如:abc,xyz,pqr
.
它应该拒绝以下值:,
,sample
text1,text2,
For example: abc,xyz,pqr
.
It should reject values like: ,
,sample
text1,text2,
我还需要接受分号分隔的值.有人可以为此建议一个正则表达式模式吗?
I also need to accept semicolon separated values also. Can anyone suggest a regex pattern for this ?
推荐答案
最简单的形式:
^\w+(,\w+)*$
我只需要限制字母.我该怎么办?
I need to restrict only alphabets. How can I do that ?
使用正则表达式(包括示例Unicode字符范围):
Use the regex (example unicode chars range included):
^[\u0400-\u04FFa-zA-Z ]+(,[\u0400-\u04FFa-zA-Z ]+)*$
示例用法:
public static void main (String[] args) throws java.lang.Exception
{
String regex = "^[\u0400-\u04FFa-zA-Z ]+(,[\u0400-\u04FFa-zA-Z ]+)*$";
System.out.println("abc,xyz,pqr".matches(regex)); // true
System.out.println("text1,text2,".matches(regex)); // false
System.out.println("ЕЖЗ,ИЙК".matches(regex)); // true
}
这篇关于正则表达式模式接受逗号或分号分隔的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文