其他教程

其他教程

Products

当前位置:首页 > 其他教程 >

Codeception URL正则表达式

GG网络技术分享 2025-03-18 16:15 0


问题描述:

So the official documentation says absolutely nothing about the tilde sign (~) used in regexp patterns, for example as in the seeCurrentUrlMatches() function. Yet, it is used extensively in the documentation.

In the exmample, the regexp looks like this:

    $I->seeCurrentUrlMatches(\'~$/users/(\\d+)~\');

  • Without the tilde sign, it gives an error. eg. grabFromCurrentUrl(\'(\\d+)\') returns:

    \" (\\d+)\" Fail Nothing to grab. A regex parameter required.

  • The end-of-string regexp operator\'s ($)

    position makes no sense (I presume the dollar sign has a completely

    different meaning in this context /my guess would be shorthand for the home path/.)
  • In standard regexp, the ~ captures the character itself

My conclusion is the regexp pattern that Codeception expects from you is clearly non-standard. So what does the ~ and $ do? Where can I find a throughout documentation or article about this?

图片转代码服务由CSDN问答提供

感谢您的意见,我们尽快改进~

功能建议

所以官方文档中没有提到regexp中使用的波形符号(〜</ code>) 模式,例如在 seeCurrentUrlMatches()</ code>函数中。 然而,它在文档中被广泛使用。</ p>

在例子中,正则表达式如下所示:</ p>

  $ I-&gt;  seeCurrentUrlMatches(\'〜$ / users /(\\ d +)〜\'); 

</ code> </ pre>

  • 如果没有波形符号,则会出错。 例如。 grabFromCurrentUrl(\'(\\ d +)\')</ code>返回:

    “(\\ d +)”无法抓取任何内容。 需要正则表达式参数。</ p>

    </ blockquote> </ li>

  • 字符串结尾regexp运算符( $ </ code>)

    position没有意义(我认为 美元符号在此上下文中具有完全不同的含义 /我的猜测将是主路径/ </ em>的简写。)</ li>

  • 在标准正则表达式中,〜 </ code>捕获字符本身</ li>

    </ ul>

    我的结论是Codeception期望你的正则表达式模式显然是非标准的。 那么〜</ code>和 $ </ code>是做什么的呢? 我在哪里可以找到关于此的文档或文章?</ p>

    </ div>

    网友观点:

    Codeception doesn\'t use any special regex functions.

    seeCurrentUrlMatches method calls PhpUnit\'s assertRegexp method which uses preg_match under the hood.

    preg_match uses pattern delimiters. Delimiter can be any non-alphanumeric character.

    / is the most common delimiter, but it is inconvenient for matching URL, because you would have to escape a lot of slashes in URL, so using ~ as a delimiter avoids need to escape.

    $ at the beginning of pattern is probably a simple mistake in example.

    url正则表达式口诀

    找了好久的正则表达很强大,附件为正则教程用的CHM文件。

    正则其实也势利,削尖头来把钱揣; (指开始符号^和结尾符号$)
    特殊符号认不了,弄个倒杠来引路; (指\\. \\*等特殊符号)
    倒杠后面跟小w, 数字字母来表示; (\\w跟数字字母;\\d跟数字)
    倒杠后面跟小d, 只有数字来表示;
    倒杠后面跟小a, 报警符号嘀一声;
    倒杠后面跟小b, 单词分界或退格;
    倒杠后面跟小t, 制表符号很明了;
    倒杠后面跟小r, 回车符号知道了;
    倒杠后面跟小s, 空格符号很重要;
    小写跟罢跟大写,多得实在不得了;
    倒杠后面跟大W, 字母数字靠边站;
    倒杠后面跟大S, 空白也就靠边站;
    倒杠后面跟大D, 数字从此靠边站;
    倒框后面跟大B, 不含开头和结尾;

    单个字符要重复,三个符号来帮忙; (* + ?)

    0 星加1 到无穷,问号只管0 和1; (*表0-n;+表1-n;?表0-1次重复)

    花括号里学问多,重复操作能力强; ({n} {n,} {n,m})

    若要重复字符串,园括把它括起来; ((abc){3} 表示字符串“abc”重复3次 )

    特殊集合自定义,中括号来帮你忙;

    转义符号行不通,一个一个来排队;

    实在多得排不下,横杠请来帮个忙; ([1-5])

    尖头放进中括号,反义定义威力大; ([^a]指除“a”外的任意字符 )

    1竖作用可不小,两边正则互替换; (键盘上与“\\”是同一个键)

    1竖能用很多次,复杂定义很方便;

    园括号,用途多;

    反向引用指定组,数字排符对应它; (“\\b(\\w+)\\b\\s+\\1\\b”中的数字“1”引用前面的“(\\w+)”)

    支持组名自定义,问号加上尖括号; (“(?<Word>\\w+)”中把“\\w+”定义为组,组名为“Word”)

    园括号,用途多,位置指定全靠它;

    问号等号字符串,定位字符串前面; (“\\b\\w+(?=ing\\b)”定位“ing”前面的字符串)

    若要定位串后面,中间插个小于号; (“(?<=\\bsub)\\w+\\b”定位“sub”后面的字符串)

    问号加个惊叹号,后面跟串字符串;

    PHPer都知道, !是取反的意思;

    后面不跟这一串,统统符合来报到; (“\\w*d(?!og)\\w*”,“dog”不符合,“do”符合)

    问号小于惊叹号,后面跟串字符串;

    前面不放这一串,统统符合来报到;

    点号星号很贪婪,加个问号不贪婪;

    加号问号有保底,至少重复一次多;

    两个问号老规矩,0次1次团团转;

    花括号后跟个?,贪婪变成不贪婪;

    还有很多装不下,等着以后来增加

    原创拔戒排骨

标签:

提交需求或反馈

Demand feedback