简体   繁体   English

正则表达式,允许字母和仅出现一次选择的特殊字符

[英]Regular expression to allow alphabets and onle one occurrence of select special characters

I need regex for name field. 我需要正则表达式的名称字段。 Name can only be alphabets. 名称只能是字母。 The other requirements for the name field are that it can contain but it is not necessary to have, any one of these characters: an apostrophe (') or a pound (#) or a hyphen (-) I have this so far 名称字段的其他要求是它可以包含但不必包含以下任何一个字符:撇号(')或英镑(#)或连字符(-)到目前为止,我已经知道了

 [\\w]*[\'#-]?

This checks the special character but only at the end of the string. 这将检查特殊字符,但仅在字符串末尾。 For example 例如

JohnO'Connell -> returns false -> should return true

JohnOConnell' -> returns true -> expected behavior
JohnOConnell# -> returns true -> expected behavior
JohnOConnell- -> returns true -> expected behavior
JohnOConnell-# -> returns false -> expected behavior

How does this have to be updated to ? 如何将其更新为?

Thanks 谢谢

You are almost there: 您几乎在那里:

^[a-zA-Z]+['#-]?[a-zA-Z]*$

Just making the ' , # , - is optional at the middle. 仅使'#-在中间是可选的。

这个允许任何unicode字母:

^\pL+['#-]?\pL+$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM