flutter Textfield Inputformatters不会与我的自定义发行
我想将这种输入允许到我的文本字段:
123
*123#
*123*4#
因此,我创建和测试了Regexr
网站此REGEX:
\ ** \ d+\ ** \ ** \ d+d+\#?
但是,当我尝试键入时,在使用的文本字段
代码中键入什么:
...
keyboardType = TextInputType.phone;
// to allow digits with asterik and hash
final regex = RegExp(r'\**\d+\**\d+\#?');
inputFormatters = [FilteringTextInputFormatter.allow(regex)];
return TextField(
...
keyboardType: keyboardType,
inputFormatters: inputFormatters,
);
I want to allow this kind of input to my text field:
123
*123#
*123*4#
so I created and tested RegExr
website this regex:
\**\d+\**\d+\#?
but when i try to type nothing is typed in the text field
code of using:
...
keyboardType = TextInputType.phone;
// to allow digits with asterik and hash
final regex = RegExp(r'\**\d+\**\d+\#?');
inputFormatters = [FilteringTextInputFormatter.allow(regex)];
return TextField(
...
keyboardType: keyboardType,
inputFormatters: inputFormatters,
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您还想将变体与单个数字匹配,例如
*1#
您可以使用负面的lookahead不包括不存在的内容:说明
^字符串的开始
(?!
负面lookahead,断言右边是什么。*\*[*#]
匹配**
或*#
|
或\ d*#$
在字符串结束时匹配可选数字和#
[*\ d]*#?
匹配可选*
字符或数字和可选#
$
字符串的结尾请参见a Regex Demo 。
If you also want to match a variation with a single digit like
*1#
you might use a negative lookahead excluding what can not be present:Explanation
^
Start of string(?!
Negative lookahead, assert what to the right is not.*\*[*#]
Match either**
or*#
|
Or\d*#$
Match optional digits and#
at the end of the string)
Close lookahead[*\d]*#?
Match optional*
chars or digits and optional#
$
End of stringSee a regex demo.
您可以使用
regex demo 。
详细信息:
^
- String的开始\*?
- 可选*
char <(?:\ code) d+\*?(?:\ d+#?)?)?
-\ d+
- 一个或多个数字\*?
- 可选*
(?:\ d+#?)?
- 一个或多个数字的可选顺序和一个可选的#
charYou can use
See the regex demo.
Details:
^
- start of string\*?
- an optional*
char(?:\d+\*?(?:\d+#?)?)?
- an optional sequence of\d+
- one or more digits\*?
- an optional*
(?:\d+#?)?
- an optional sequence of one or more digits and an optional#
char$
- end of string.