5 ms·
can't overstate how nice raku is, bit sharper learning curve but still raku: ^ ( \d+ <?{ $/ <= 255 }> ) ** 4 % '.' $ slow raku: ^ ( @(0..255) )
by asojfdowgh 4y ago
can't overstate how nice raku is, bit sharper learning curve but still
raku: ^ ( \d+ <?{ $/ <= 255 }> ) ** 4 % '.' $
slow raku: ^ ( @(0..255) ) ** 4 % '.' $
rulex: <% ( :( range '0'-'255' ) '.' ){3} :( range '0'-'255' ) %>
regex: ^(?:(0|1[0-9]{0,2}|2(?:[0-4][0-9]?|5[0-5]?|[6-9])?|[3-9][0-9]?)\.)((0|1[0-9]{0,2}|2(?:[0-4][0-9]?|5[0-5]?|[6-9])?|[3-9][0-9]?))$
first one running code in the middle of the regex, to see if it matches a mathematical property, the second, running code to generate a [0|1|2|3|...|254|255] equivalent, which also matches as tightly as the rulex/regex code (e.g. no 00 allowed)
- donatj 4y agoSeems like a case where I would just \d+\.\d+\.\d+\.\d+ and validate it in code
- asojfdowgh 4y agocan still do that logic too, without leaving the parsing step, per say /^ (\d+) ** 4 % \. <?{ $/[0].all <= 255 }> $/ or if wanting it really explicit /^ (\d+) \. (\d+) \. (\d+) \. (\d+) <?{ $/.all <= 255 }> $/
- lbriner 4y agoNone of those seem much easier to understand than the others. I suspect this is a classic example of "let's take something that is really messy in pcre and compare it other more brief equivalents" but I could easily turn the regex into something more readable and I can find more help for the standard regex syntax than I can for any alternative. As other's have said, it is a minor arguably cosmetic improvement and something that is likely easier just to use a visual helper like regex101 to help you construct.