自从我在PHP 7.2环境中开始测试以来,我的一些基于语言环境的测试失败了,因为locale de_CH的货币格式发生了变化。
5'123.81
5’123.81
我决定"改善"我的测试,能够分段不同的分段。天真的方法是直接测试unicode字符。
$this->assertRegExp("@5['’]123\.81@",'5’123.81');
当然 - 失败了。后来我读到了PCRE中unicode字符的使用情况(这里是http://www.regular-expressions.info/unicode.html),并且我不能使用Unicode代码点\u
,而是必须使用UTF-8 \x
。
但我不知道如何在小组中使用它:
$rightSingleQuotationMarkCode = '\xe2\x80\x99';
// this is fine
$this->assertRegExp("@5{$rightSingleQuotationMarkCode}123\.81@",'5’123.81');
// this is not
$this->assertRegExp("@5['{$rightSingleQuotationMarkCode}]123\.81@",'5’123.81');
消息:
Failed asserting that '5’123.81' matches PCRE pattern "@5['\xe2\x80\x99]123\.81@"
据我所知,该组[]不支持将多字节字符组合在一起。并找到了像
这样的工作解决方案$testRegExp = "@5'?(?:{$rightSingleQuotationMarkCode})?123\.81@";
$this->assertRegExp($testRegExp,'5’123.81');
$this->assertRegExp($testRegExp,'5\'123.81');
但它在解决这个问题的方式上看起来不对,我的问题是,如果在PHP PCRE中有更好或更顺畅的集成解决方案吗?