简体   繁体   English

正则表达式匹配无法正常工作

[英]Regex matching doesn't work as expected

I'm trying to match every string (url) starting with "string" and not ending in -[number] 我正在尝试匹配以“ string”开头但不以-[number]结尾的每个字符串(url)

So I made this regex 所以我做了这个正则表达式

string/?.*(?!-[0-9]*)

which, for what I understood, is supposed to be read as: 据我所知,应理解为:

match every string starting with "string", having possibly a '/' after it, having any string after it not including '-' followed by any number or numbers. 匹配以“ string”开头的每个字符串,后跟一个“ /”,后跟任何字符串,不包括“-”,后跟任意数字。

here's my test strings 这是我的测试字符串

string/kkk/aaa/sss/ddd-123
string/kkk/aaa/sss/ddd
string/kkk/aaa/sss
string/kkk/aaa
string/kkk
string/
string/kkk/
string/kkk/aaa/
string/74002

the regex just match everything, no matter what. regex可以匹配所有内容,无论如何。

Could someone tell me where I went wrong ? 有人可以告诉我我哪里出问题了吗?

In your expression you have added .* at the before the lookahead. 在表达式中,在前行之前的。处添加了.*

This means * will skip all available characters matching . 这意味着*将跳过所有可用的字符匹配. before proceeding to the next step - This in essence is everything and hence your expression matches everything. 在继续下一步之前-本质上是所有内容,因此您的表情与所有内容都匹配。 This is usually used when you want to return everything after a pattern. 当您要在模式之后返回所有内容时,通常使用此方法。

You need to move the .* in to the lookahead statement or use a non-greedy version. 您需要将.*移至lookahead语句中或使用非贪婪版本。

Try something like 尝试类似

^string/?(?!.+?-\d+$).*

The above will match all string that do not end with - and digits. 上面的代码将匹配所有不以-和数字结尾的字符串。 It will also return the entire string for instances where the pattern is matched. 对于匹配模式的实例,它还将返回整个字符串。 I have used the non-greedy .+? 我已经使用了非贪婪的.+? here to avoid confusion as well as adding the ^ start and $ end of line selectors. 为了避免混淆,还添加了行选择器的^开始和$结束。 The .* matches all the characters if the pattern is successful. 如果模式成功,则.*匹配所有字符。

Try this : 尝试这个 :

$str  = "string/kkk/aaa/sss/ddd123";
echo preg_match("/^string\/(?!.*-\d+$)/",$str);

Your problem is you don't force the regular expression to match the whole string. 您的问题是您不强制正则表达式匹配整个字符串。

For example, take this line: 例如,使用以下行:

string/kkk/aaa/sss/ddd-123

In this case .* will simply match everything after string/ including the -123 . 在这种情况下, .*将仅匹配string/之后的所有内容,包括-123 A non-greedy match would match everything excluding the d-123 . 非贪婪的匹配将匹配除d-123之外的所有内容。 In either case it succeeds in not finding what's given in the negative lookahead. 无论哪种情况,它都能成功地找到否定的前瞻。

You'll have to force the regular expression to match the string end ( $ ) (and probably the string start ( ^ ) as well: 您必须强制正则表达式匹配字符串end( $ )(也可能匹配字符串start( ^ ):

^string/?.*(?!-[0-9]*)$

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

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