首页 > 解决方案 > 如何向正则表达式添加规则

问题描述

美好的一天我有这个表单元素

   <?php echo new \App\Http\Render\Form\Element\Text([
                      'value' => (isset($this->entity['str'])) ? $this->entity['str'] : '',
                      'elementName' => 'item[str]',
                      'elementID' => 'str',
                      'required' => true,
                      'class' => 'form-control',                   
                        'attributes' => [
                            'data-msg-required' => 'Bitte geben Sie Ihre Straße inkl. Hausnummer an.',//Bitte geben Sie Ihre Stra&szlig;e ein.
                            'regex' => "[/.*?(\d+)$/]",
                            'data-rule-minlength'=>"6",
                            'data-msg-minlength' => 'Bitte verwenden Sie keine Zahlen und Sonderzeichen.',
                            'data-msg-regex' => 'Bitte geben Sie Ihre Straße inkl. Hausnummer an.',
                        ],
                      'placeholder'=>" "
                  ]) ?>

如您所见,我使用正则表达式来记录字符串后面的数字 epsen 时的错误。请告诉我为什么在字符串后面加一个点时看不到错误?比如这个案例“Strabe”。当字符串末尾的字符串中没有数字时,如何设置正则表达式以记录错误?

标签: phpregex

解决方案


Your regex pattern [/.*?(\d+)$/] is enclosed in [ and ], which means a class of characters, so a string that contains any of the characters in between the square brackets would match your regex pattern, including a dot .. Slashes (/) are also not needed as an enclosure.

You should simply write 'regex' => ".*?(\d+)$" instead.


推荐阅读