guild icon
Toit
#Where is the regex?
Thread channel in help
hhv0001
hhv0001 01/14/2024 01:26 PM
Hi, I'm trying to find a package that supports regexes but I can't find it. Nobody wanted regexes so far? Or am I not that good at looking for packages? It seems not to be built in to the Toit language either.

Thanks for any hints on where to look.
floitsch
floitsch 01/14/2024 01:27 PM
@erikcorry wrote an implementation but hasn't released it as a package yet.
floitsch
floitsch 01/14/2024 01:27 PM
I did a minimal (and slow) version as well.
floitsch
floitsch 01/14/2024 01:33 PM
GitHub Gist: instantly share code, notes, and snippets.
floitsch
floitsch 01/14/2024 01:33 PM
We want to add Regex eventually to the language as well. Just didn't have the time yet.
hhv0001
hhv0001 01/14/2024 01:33 PM
OK, thanks again. There's no rush so I'll wait patiently. Whether or not is is fast is not that important to me. Functionality also is not that important, I just wanted to match simple URLs, e.g. "/hetis/true" and "/hetis/false" and some RGB values maybe. Simple stuff.
hhv0001
hhv0001 01/14/2024 01:33 PM
OK, I'll have a look!
floitsch
floitsch 01/14/2024 01:33 PM
That should work with the gist-version.
hhv0001
hhv0001 01/14/2024 01:34 PM
Great!
floitsch
floitsch 01/14/2024 01:34 PM
Here are a few test-cases:
r := regex.parse "a|b" expect (r.match "a") expect (r.match "b") expect-not (r.match "c") r = regex.parse "^a|b\$" expect (r.match "a") expect (r.match "b") expect-not (r.match "c")
hhv0001
hhv0001 01/14/2024 01:34 PM
Excellent, just what I need it seems. :👍:
floitsch
floitsch 01/14/2024 01:34 PM
It doesn't support captures, though.
hhv0001
hhv0001 01/14/2024 01:35 PM
Aaah... :😦:
floitsch
floitsch 01/14/2024 01:35 PM
So if you need to get the values out, then you need Erik's library.
floitsch
floitsch 01/14/2024 01:35 PM
Contribute to erikcorry/toit-dartino-regexp development by creating an account on GitHub.
hhv0001
hhv0001 01/14/2024 01:36 PM
:🙂: No worries. I can wait. I'll have a look at Erik's library.
floitsch
floitsch 01/14/2024 01:36 PM
At least I hope he has implemented captures.
floitsch
floitsch 01/14/2024 01:36 PM
Source definitely talks about captures.
hhv0001
hhv0001 01/14/2024 01:38 PM
It does, it's also tested it seems, it might work.
floitsch
floitsch 01/14/2024 01:41 PM
I just pinged Erik, and asked him to publish his package, even if it's not completely finished yet.
👍1
erikcorry
erikcorry 01/14/2024 04:41 PM
erikcorry
erikcorry 01/14/2024 04:41 PM
It's Dart-compatible, which means it's JavaScript-compatible.
erikcorry
erikcorry 01/14/2024 04:41 PM
ES5 version.
erikcorry
erikcorry 01/14/2024 04:43 PM
The documentation is a bit lacking, but see the test.
erikcorry
erikcorry 01/14/2024 04:47 PM
Note that we don't have raw strings, so you have to double backslashes and dollar signs.
If you wanted to match /^\d\d\d\d-\d\d-\d\d$/ (an ISO date) then you would have to write re := RegExp "^\\d\\d\\d\\d-\\d\\d-\\d\\d\$"
hhv0001
hhv0001 01/14/2024 05:41 PM
I'll give it a go, thanks guys!
27 messages in total