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
-
Rocket science is also not that hard, you only need to learn the science 😄
-
You're compiling those every time your function is called.
But yes, regex ftw 🤘
some would argue that writing a parser as a finite state machine is the proper way over regex, but I don't know what your application is for -
Sumafu22425yYeah, I should outsource the regexs to global variables and compile them just once.
@magicMirror I just managed to reduce the 13 regexs to just 9.
@beegC0de this is a parser for a range input string. The input can either be just a number or a range in four different kinds. And the numbers can either be a normal decimal or a hexadecimal floating constant. -
@Sumafu you can do better...
Also - string compare is much faster then a regex. just saying... -
Sumafu22425y@magicMirror yes, string comparison would we maybe faster, but in this case it would be a bunch of code that no one wants to maintain, because I have to check if the number is a normal decimal or a hexadecimal floating constant (something like 0x1.b7p-1). Using regex in this case is much more easier and shorter and more maintainable.
-
Sumafu22425yJust a hexadecimal floating constant would be easy, but it could be something like "0x1.b7p-1" or "0x1.b7p-1-0x2.b7p-1", or "<0x1.b7p-1" or ">=0x1.b7p-1", or ">0x1.b7p-1,<=0x2.b7p-1" and so on. And the same with normal numbers.
-
@Sumafu 🤦
you can decide if you need to regex the string using a string compare.
atm, worst case, you try to match regex 9 times per string. you can - check using a string compare, before the regex, reducing the regex operations.
hammer, and nails. -
Sumafu22425yIt seems that I misunderstood your meaning of string compare. I thought you meant comparing just plain strings.
Now that you saying it, I could check if the string starts with "not" or ">" or "<" or contains a ",". These strings should be unique and should be suitable for a rough differentiation. -
@Sumafu you know what we say about regexes?
« You have one problem. You use a regex to solve it. Now you have two problems. » -
Is this standard syntax for regex? Or is it some JS bastardization... Also ^not looks weird...
-
Sumafu22425y@arcsector This is Golang, and the ^not just means that the line starts with the string "not", this is common regex
-
@Sumafu my bad; i always forget that when the caret is outside the brackets it doesnt mean "not".
-
For each line, there should be a comment that describes in clear text which kind of string it is intended to match, like an example string. Just for maintainability.
The actual difficulty with regexp is not matching strings. It is making sure that there are no unintended matches. Since this boils down to proving a negative, it is hard to test.
But still.. bravo! ^^ -
Sumafu22425y@Cystal Yes, regex is slow. This is the problem if you can write regex without googling, because you came up very fast with a regex solution 😅
-
What the fuck is this shit and why does it exist?
[This might be my favourite sentence I've ever come up with] -
@electrineer searching with that sentence got me this: https://deviantart.com/organicgrani...
I expected something better. -
I'm not familiar with the language, but isn't there a better way to write this? Giving maintenance to this later will be hard, it's kinda hard to read.
-
Sumafu22425y@darksideofyay yes, currently I try to write it without regex, just with plain (sub)string comparing and checking.
But to check this amount of regex with a different handling of each possibility this is the shortest way (although the shortest way is not necessarily the most readable).
But Go has in fact sometimes a very special syntax. -
Sumafu22425y@darksideofyay i used a shorthand for calling a function and check it's return value.
-
hjk10156965y@darksideofyay yes there are far better ways to write this...
@arcsector it's mostly PCRE but they removed some slow features like forward lookup. It's also used in Google sheets. Checkout the regex function docs and notice the warning. -
hjk10156965y@Sumafu don't worry too much about regex performance issues. If it is truly a pattern you are looking for that is hard to express in an algorithm than regex might be the fastest solution. It's highly optimized code.
Although this is not stackoverflow of you send me a copy of the code I can see if I can get rid of that if else jungle here. You most likely need a more data structure driven approach -
One trick for performance is to do some cheap check for early abort.
That works if you have a clear pattern and you also know how most of your non-matching data don't match.
Related Rants
I just wrote this piece of code. Without googling. Call me regex king!
But in fact regex is not that hard, you just have to learn the syntax 😄
rant
regex