简体   繁体   English

正则表达式检测连续的字符

[英]Regex to detect consecutive characters

Hope someone can help me with a regular expression to validate names. 希望有人可以帮助我使用正则表达式来验证名称。 The criteria are that the string must be between 1 and 30 characters in length and will allow the following: uppercase alpha, lowercase alpha, space, apostrophe, full stop (or period) and hyphen. 标准是字符串的长度必须介于1到30个字符之间,并允许以下内容:大写字母alpha,小写字母alpha,空格,撇号,句号(或句点)和连字符。

I've got a regex that will do this, but the complication is that the "special" characters (space, apostrophe, full stop, hyphen) may not be consecutive. 我有一个正则表达式会这样做,但复杂的是“特殊”字符(空格,撇号,句号,连字符)可能不连续。 So you could not have this: "Smithers-'Jones" (hyphen followed by apostrophe), or this: "Smithers –Jones" (space followed by hyphen), or this "O''Reilly" (consecutive apostrophes). 所以你不能拥有这个:“Smithers-'Jones”(连字符后跟撇号),或者说:“Smithers -Jones”(空格后跟连字符),或者这个“O''Reilly”(连续的撇号)。

From what I've read so far, I think I need to use back-references in some way, but I haven't managed to get anything working yet. 从我到目前为止所读到的内容来看,我认为我需要以某种方式使用反向引用,但我还没有成功地完成任何工作。 I've seen examples that detect repeating characters, but this is not quite the same. 我见过检测重复字符的例子,但这并不完全相同。

Any help would be appreciated. 任何帮助,将不胜感激。

This regex should work: 这个正则表达式应该工作:

^(?!.*?[ '.-]{2})[A-Za-z0-9 '.-]{1,30}$

Working Demo: http://regex101.com/r/pJ3hJ9 工作演示: http//regex101.com/r/pJ3hJ9

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

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