正则表达式模式接受逗号或分号分隔值

Kri*_*P V 5 java regex

我需要一个正则表达式模式,它只接受输入字段的逗号分隔值.

例如:abc,xyz,pqr.它应该拒绝以下值:, ,sample text1,text2,

我还需要接受以分号分隔的值.任何人都可以为此建议正则表达式模式吗?

acd*_*ior 8

最简单的形式:

^\w+(,\w+)*$
Run Code Online (Sandbox Code Playgroud)

在这里演示.


我只需要限制字母表.我怎样才能做到这一点 ?

使用正则表达式(包括示例unicode字符范围):

^[\u0400-\u04FFa-zA-Z ]+(,[\u0400-\u04FFa-zA-Z ]+)*$
Run Code Online (Sandbox Code Playgroud)

在这里演示这个.

用法示例:

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
}
Run Code Online (Sandbox Code Playgroud)

Java演示.