简体   繁体   English

正则表达式-匹配字符串模式

[英]Regex - Match a string pattern

I need a regex that will match only strings of this kind 我需要一个仅匹配这种字符串的正则表达式

Match these strings: 匹配以下字符串:

edo-apple-iphone-5s-i-gold-16

DON'T match these strings: 不要匹配以下字符串:

edo-apple-iphone-5s-i-gold-16-edo-staff-connect-24

The main difference between the 2 strings is either a connect-24 OR handset-24 added to the end of the string 2个字符串之间的主要区别是在字符串末尾添加了connect-24handset-24

I have written a Regex, but it seems to match both strings: 我写了一个正则表达式,但似乎匹配两个字符串:

^edo[a-z1-9\-]*

How do i modify this to not accept if connect-24 OR handset-24 is in the string? 如果在字符串中connect-24handset-24我如何修改它以不接受?

The regex that matches the requirements: Not accept if connect-24 OR handset-24 is in the string 符合要求的正则表达式:如果字符串中的connect-24 OR听筒-24,则不接受

^(?:(?!connect-24|handset-24).)*$

DEMO DEMO

Here is a regular expression that would satisfy the requirement as stated: 这是一个满足以下要求的正则表达式:

.{32,}

Clearly, the strings you want to match are all longer than the ones you don't want to match. 显然,您要匹配的字符串都比您不想匹配的字符串长。 This regex matches only those strings that are 32 characters or longer, while edo-apple-iphone-5c-i-yellow-32 , the longest non-matching string, has only 31 characters. 此正则表达式仅匹配32个字符或更长的字符串,而最长的不匹配字符串edo-apple-iphone-5c-i-yellow-32只有31个字符。

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

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