正则表达式将通过这些条件?

时间:2016-10-13 20:21:24

标签: c# regex

如何编写正则表达式以传递以下内容?

LastName
LastName SecondLastName
LastName-SecondLastName
LastName, F
LastName SecondLastName, F
LastName-SecondLastName, F
LastName, FirstName
LastName SecondLastName, FirstName
LastName-SecondLastName, FirstName
LastName SecondLastName, FirstName SecondFirstName
LastName SecondLastName, FirstName-SecondFirstName
LastName-SecondLastName, FirstName-SecondFirstName

同时,至少以下内容应该失败:

Any special Character (,?!#@%* etc) at the begining
,FirstName
, FirstName
LastName,
LastName,Any special Character (,?!#@%* etc)
LastName,FirstName

基本上,在逗号之后,应该有一个空格和字母字符

资源:

https://regex101.com/

[A-Za-z]+,\s+[A-Za-z]+

提前致谢! :)

1 个答案:

答案 0 :(得分:1)

你怎么样尝试:

^[a-zA-Z]+([- ][a-zA-Z]+)?(, [a-zA-Z]+([- ][a-zA-Z]+)?)?$

示例:https://regex101.com/r/ipffrk/3