Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
This sounds like a job for a simple for-loop to me, actually.
Regular grammar pattern match => regex
Counting => loop
Recursive/nested structure => parser -
@RememberMe Ah, but a loop just seems so inelegant when a regex can handle the whole lot...
-
@kenogo I agree in most places, but sometimes it's just so easy to slot one in rather than writing code that it almost begs to be done - HTML5 form validation for example.
-
@beegC0de Nope, up to 5 digits anywhere in any combination (so "12 is better than 123" would match, as would "12345 is great", but "123 and 456" would not.)
-
@AlmondSauce I think a for loop would be a little more understandable to do this man. Unless you're doing code golf?
-
@beegC0de Yeah, in the end I undid said regex and just looped it. Still wish I was better with them though.
Related Rants
More like a confession.
Had a task that involved invalidating a string if it contained more than five digits (anywhere in the string.)
"No problem", I thought. That sounds like a simple regex!
^(\D*\d?\D*){5}$
Turns out catastrophic backtracking had other ideas, and I revert to my usual "atomic grouping fudge that will hopefully work without me really understanding what's going on" of:
^(?>\D*\d?\D*){5}$
...anyone else shamefully follow this mantra, or am I the only one that hasn't skilled up properly on regexes...
question
regex
regular expression