简体   繁体   English

一切,但[和]之间的一切

[英]everything, but everything between [ and ]

I'm trying to match everything, but everything between [ and ]. 我试图匹配所有东西,但[和]之间的所有东西。

everything between [ and ] is [和]之间的一切都是

\[.+\]

everything, but everything between [ and ] is 一切,但[和]之间的一切都是

[^(\[.+\])]+

The search text is 搜索文本是

valid[REGEX_EMAIL|REGEX_PASSWORD|REGEX_TEST]

It matches "valid" and "REGEX_EMAIL|REGEX_PASSWORD|REGEX_TEST". 它匹配“valid”和“REGEX_EMAIL | REGEX_PASSWORD | REGEX_TEST”。

It is supposed to match "valid", but not "REGEX_EMAIL|REGEX_PASSWORD|REGEX_TEST". 它应该匹配“有效”,但不匹配“REGEX_EMAIL | REGEX_PASSWORD | REGEX_TEST”。

How to solve? 怎么解决?

I want my php validation class to be like the CodeIgniter one... 我想我的php验证类就像CodeIgniter一样......

[^(\\[.+\\])]+ doesn't mean what you think it means. [^(\\[.+\\])]+并不代表您认为的含义。

Literally, it means "match any character except any one of these ()[.+] one or more times." 从字面上看,它意味着“匹配除了这些()[。+]中的任何一个以外的任何字符一次或多次。”

[] are a character set, matching against one of the characters inside that set (or not matching them if it starts with a ^ ) []是一个字符集,与该集合中的一个字符匹配(如果以^开头,则不匹配它们)

I believe you'll find your answer in what is called negative lookahead. 我相信你会在所谓的负向前瞻中找到答案。 It allows you to include patterns in your search without actually including them in your match. 它允许您在搜索中包含模式,而不会在匹配中包含它们。

/^.*(?!\[.+\])$/

(?! ... ) being the negative lookahead part. (?!...)是负面的前瞻部分。

$string = 'valid[REGEX_EMAIL|REGEX_PASSWORD|REGEX_TEST]';

preg_match('#(\S+)\[.+?\]#', $string, $match);

echo $match[1];

Try this based on the data you're using 'valid[REGEX_EMAIL|REGEX_PASSWORD|REGEX_TEST]' 根据您使用的数据“有效[REGEX_EMAIL | REGEX_PASSWORD | REGEX_TEST]”尝试此操作

^[^[]+   

It will return "valid". 它将返回“有效”。

Depending no the language you're using, you might need to escape the [, so write it like this ^[^\\[]+ 根据您使用的语言,您可能需要转义[,所以将其写为^[^\\[]+

This regex assumes that there will never be a "[" in the text preceding [REGEX_EMAIL|REGEX_PASSWORD|REGEX_TEST] 这个正则表达式假定在[REGEX_EMAIL | REGEX_PASSWORD | REGEX_TEST]之前的文本中永远不会有“[”

I tested this using Eric Gunnerson's RegexWorkbench for .NET 我使用Eric Gunnerson的RegexWorkbench for .NET对此进行了测试

I've checked the source code of CI's Validation class now. 我现在已经检查了CI验证类的源代码。

They allow rules to be set like 它们允许设置规则

array('field' => "valid|length[5]|foo|callback_bar")

I didn't see any nested square brackets or pipes inside the square brackets. 我没有在方括号内看到任何嵌套的方括号或管道。 The Docs clearly say, you may have only one param. 文件清楚地说,你可能只有一个参数。 The string is set internally to $_rules . 该字符串在内部设置为$_rules When validating, the string will be exploded into an array first, so the above would evaluate to four $rules . 验证时,该字符串将被exploded成一个阵列的第一,所以上述评价四个$rules

'field' => array('valid', 'length[5]', 'foo', 'callback_bar')

They then loop through the array, checking if the $rule is a callback with substr() . 然后,它们遍历数组,检查$rule是否是带substr()的回调。 Then they check if there is square brackets in the $rule with the pattern "/(.*?)\\[(.*?)\\]/" and if so, take it off the $rule and store the inner part of the brackets as $param . 然后他们检查$rule中的方括号是否带有"/(.*?)\\[(.*?)\\]/"模式,如果有,请将其从$rule取出并存储内部部分括号为$param And finally, they just execute the $rule as a variable function with the detected param, eg $rule(POST[$field], 5) ; 最后,他们只使用检测到的参数执行$rule作为变量函数,例如$rule(POST[$field], 5) ;

As you can see, they are not splitting everything in one go. 正如你所看到的,他们并没有一次性分裂所有东西。 This does not answer your question, but shedding some light on CI's internal logic to get their Validator running might help you rethink your approach. 这不能解答您的问题,但是对CI的内部逻辑有所了解以使其Validator运行可能有助于您重新思考您的方法。

Opinion : I'd like to add that their approach is terrible. 意见 :我想补充一点,他们的方法很糟糕。 Validator Chains are prime candidates for the Command Pattern . 验证器链是命令模式的主要候选者。 Sure, it's nice to specify validators by small and compact strings, but you pay this by a lot of ugly string juggling, when it comes to actually running the chain. 当然,通过小而紧凑的字符串来指定验证器是很好的,但是当你真正运行链时,你可以通过很多丑陋的字符串来解决这个问题。 Have a look at how Zend Framework does it or look at PHPs native filter functions . 看看Zend Framework如何做到这一点或者看看PHP的本机过滤器功能

This is three different kinds of text, if I understand correctly: 如果我理解正确,这是三种不同的文本:

  1. From the beginning up to the first [ 从一开始到第一次[
  2. Between a ] and a [ 介于]和[之间]
  3. From the last ] to the end 从最后一个到最后

Given this, there is a regex for each: 鉴于此,每个都有一个正则表达式:

  1. ^([^\\x5B]*)[
  2. ]([^\\x5B\\x5D]*)[
  3. ]([^\\x5D]*)$

(x5B and x5D are the hex escapes for left and right bracket.) Note that the match of the entire expression will include the brackets that mark the boundaries; (x5B和x5D是左右括号的十六进制转义符。)请注意,整个表达式的匹配将包括标记边界的括号; sub-expression 1 gives the match excluding the bracket. 子表达式1给出不包括括号的匹配。

尝试一个积极的前瞻断言(它本身没有被捕获)

^.*(?=\[.+\])

You don't have to be a master of regular expressions, when there is Regex Tester . 当有Regex Tester时,您不必是正则表达式的主人。 Just type in some test data and play around with a regex until you get a desired result. 只需键入一些测试数据并使用正则表达式,直到获得所需的结果。 There is also Quick Reference to help you out on the right hand side. 还有快速参考可以帮助您在右侧。

Well, at least that's how I deal with them. 好吧,至少我是如何处理它们的。

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

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