I need a ruby regexp pattern that matches a string containing a letter (for simplicity say ‘a’) n times and then n at the end.
For example, it should match “aaa3”, “aaaa4” etc but not “a2” or “aaa1”, etc.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
That is not possible in regex since it is not a regular language (that’s easy to prove with the Pumping Lemma for Regular Languages). I’m not sure how much more powerful ruby regex is than a true Regular Expression, but I doubt it’s powerful enough for this. You can set a finite limit on it and state each possibility like:
Since all finite lanugages are regular, but it would be much easy to use string operations to count the number of times a letter appears and then parse for that integer in the string right after the last of that letter.