php - 如何向正则表达式添加规则
问题描述
美好的一天我有这个表单元素
<?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ß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”。当字符串末尾的字符串中没有数字时,如何设置正则表达式以记录错误?
解决方案
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.
推荐阅读
- javascript - 使用事件侦听器时如何访问父容器上的属性?
- javascript - HTML页面中的JavaScript代码是使用客户端机器的力量还是服务器的机器力量?
- azure-devops - 将值从 PowerShell 脚本任务(变量)传递到同一代理作业中的后续任务
- python - 如何在 Python 中对我的语料库使用“collocation_list”函数?
- progressive-web-apps - 当 pwa 在 laravel 上处于活动状态时,我的应用程序无法更新缓存
- ajax - 如何计算表格中的数据行
- python - spark groupby xxx 并将两行堆叠到 tuple / dict 列表中
- python - 如何从资源商店发出优先获取请求
- java - JPL7 中“断言”的等价物
- java - 这里发生了什么?