我正在尝试构建一个输入表单,该表单应允许用户输入任意数量的字母,后跟空格(不包括输入的最后一个字母)。
例如:
a b c d e f= MATCHES
f g z b= MATCHES
aa bb cd efg= DOES NOT MATCH
ab c d e f g=DOES NOT MATCH
我目前有以下内容:
[a-zA-Z]\s+|[a-zA-Z]$
似乎不起作用。
为什么这不起作用/我做错了什么?
答案 0 :(得分:2)
正则表达式应为/^([a-z]\s)+[a-z]$/i
答案 1 :(得分:0)
你走了:
^(?:[a-zA-Z]\s)+[a-zA-Z]$
# anchor it to the beginning of the line
# non capturing group
# with ONE letter and ONE space unlimited times
# followed by exactly ONE letter and the end of the line ($)
请参阅a demo on regex101.com并确保使用MULTILINE
模式(针对主播)。
答案 2 :(得分:0)
不确定是否要以任何方式更改正则表达式,但我确实找到了一个较短的正则表达式,可以使用单个字符后跟空格:
class SomeControllerFactory implements FactoryInterface
{
public function CreateService(SeviceLocatorInterface $serviceLocator)
{
$realServiceLocator = $serviceLocator->getServiceLocator();
// other things from service manager
$registrationForm = $realServiceLocator->get('FormElementManager')
->get('Path\To\My\Form\RegistrationForm');
}
return new SomeController(
// controller dependencies, including $registrationForm
);
}
答案 3 :(得分:0)
您可能想尝试这种模式:
^((?:[^ ] )+[^ ]?)$
REGEX EXPLANATION:
^ # assert line start
( # capturing group starts
(?: # 1st non-capturing group starts
[^ ] # one non-space character
) # followed by a space; 1st non-capturing group ends
+ # repeat above pattern 1 or more times
[^ ] # match a non-space character
? # 0 or 1 time
) # capturing group ends
$ # assert end of line