php - 如何验证一串数字和逗号,但不是字母字符
问题描述
如何编写用于 PHP (preg_match) 的正则表达式来仅验证数字和逗号(可选)?我想出了这个:
[,0-9]+
但它似乎不起作用。这就是我想要实现的目标:
1 -> OK
1,2 -> OK
1,2,3 -> OK
abc -> NO
1,a -> NO
1,2,a -> NO
1, a, 2 -> NO
解决方案
试试这个:^[,0-9]+$
看演示
代码:
$re = '/^[,0-9]+$/m';
$str = '1
1,2
1,2,3
abc
1,a
1,2,a
1, a, 2
';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
推荐阅读
- r - SelectInput过滤器基于R中另一个selectInput的选择
- ibm-cloud - 如何扩展 IBM Watson Assistant 的示例项目以显示文本以外的答案?
- sharepoint - 如何在线访问“APP for Sharepoint”页面
- python - 在 Oracle 插入期间将“无”字符串转换为空
- javascript - Then() 在 React 中的调度操作之后不起作用
- bash - 执行命令后调试慢速 bash
- flutter - 使用 Cloud Firestore REST API 如何使用带有 http.post() 方法的 http 包发送数据
- r - R中并排的两个饼图
- angular - Searchable select options
- java - Android SwipeRefreshLayout 返回 null