我需要一个正则表达式模式,它只接受输入字段的逗号分隔值。
例如:abc,xyz,pqr
。
它应该拒绝以下值:,
,sample
text1,text2,
我还需要接受分号分隔的值。谁能为此建议一个正则表达式模式?
最佳答案
最简单的形式:
^\w+(,\w+)*$
I need to restrict only alphabets. How can I do that ?
使用正则表达式(包括示例 unicode 字符范围):
^[\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
}
关于java - 接受逗号分隔值的正则表达式模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17311345/