28
Sumafu
5y

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 😄

Comments
  • 23
    Rocket science is also not that hard, you only need to learn the science 😄
  • 10
    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
  • 18
    That thing is ugly as fuck, and recompiles every iteration. Also - you can condense this to use less regexs.

    but yes - you can write regex!
  • 1
    Yeah, 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.
  • 2
    @Sumafu you can do better...
    Also - string compare is much faster then a regex. just saying...
  • 2
    @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.
  • 1
    Just 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.
  • 2
    @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.
  • 2
    It 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.
  • 8
    @Sumafu you know what we say about regexes?

    « You have one problem. You use a regex to solve it. Now you have two problems. »
  • 1
    Is this standard syntax for regex? Or is it some JS bastardization... Also ^not looks weird...
  • 1
    @arcsector This is Golang, and the ^not just means that the line starts with the string "not", this is common regex
  • 2
    @Sumafu my bad; i always forget that when the caret is outside the brackets it doesnt mean "not".
  • 3
    I once wrote regexes to generate regexes to generate SQL commands.
  • 2
    Dosen't regx requires extra time to process?
  • 1
    @Fabian YOU MADMAN!
  • 4
    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! ^^
  • 2
    @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 😅
  • 4
    What the fuck is this shit and why does it exist?

    [This might be my favourite sentence I've ever come up with]
  • 2
    @electrineer searching with that sentence got me this: https://deviantart.com/organicgrani...

    I expected something better.
  • 5
    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.
  • 1
    @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.
  • 1
    oh i didn't mean the regex, just the if else if else if else if
  • 0
    @darksideofyay i used a shorthand for calling a function and check it's return value.
  • 1
    Regexer is a good place to play around
  • 0
    @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.
  • 0
    @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
  • 0
    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.
Add Comment