Regular expression to count number of commas in a string

前端 未结 9 941
故里飘歌
故里飘歌 2020-11-27 05:23

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

相关标签:
9条回答
  • 2020-11-27 06:09

    Exactly 21 commas:

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

    At least 21 commas:

    ^([^,]?,){21}.*$
    
    0 讨论(0)
  • 2020-11-27 06:11

    What language? There's probably a simpler method.

    For example...

    In CFML, you can just see if ListLen(MyString) is 22

    In Java, you can compare MyString.split(',') to 22

    etc...

    0 讨论(0)
  • 2020-11-27 06:12
    .*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,
    
    0 讨论(0)
提交回复
热议问题