Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to limit the number of occurrences of a particular character in a string

Tags:

c#

regex

Is it possible using only regex to limit the number of occurrences of specific characters in a string?

For example, I want to limit the number of occurrences of $ or % or space in a string to 5 occurrences max.

Is this possible?

like image 845
Flood Gravemind Avatar asked Jan 22 '26 22:01

Flood Gravemind


1 Answers

try this expression. In this case, the character you're limiting is A

/^([^A]*A[^A]*){0,5}$/

http://rubular.com/r/X5iz5dHzgs

like image 133
Sam I am says Reinstate Monica Avatar answered Jan 25 '26 12:01

Sam I am says Reinstate Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!