计算字符串中逗号个数的正则表达式 [英] Regular expression to count number of commas in a string

查看:50
本文介绍了计算字符串中逗号个数的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何构建一个正则表达式来匹配包含任意字符但必须包含 21 个逗号的任意长度的字符串?

How can I build a regular expression that will match a string of any length containing any characters but which must contain 21 commas?

推荐答案

/^([^,]*,){21}[^,]*$/

即:

^     Start of string
(     Start of group
[^,]* Any character except comma, zero or more times
,     A comma
){21} End and repeat the group 21 times
[^,]* Any character except comma, zero or more times again
$     End of string

这篇关于计算字符串中逗号个数的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆