Where is the regex?
# help
h
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.
f
@erikcorry wrote an implementation but hasn't released it as a package yet.
I did a minimal (and slow) version as well.
We want to add Regex eventually to the language as well. Just didn't have the time yet.
h
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.
f
That should work with the gist-version.
h
OK, I'll have a look!
Great!
f
Here are a few test-cases:
Copy code
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")
h
Excellent, just what I need it seems. 👍
f
It doesn't support captures, though.
h
Aaah... 😦
f
So if you need to get the values out, then you need Erik's library.
h
🙂 No worries. I can wait. I'll have a look at Erik's library.
f
At least I hope he has implemented captures.
Source definitely talks about captures.
h
It does, it's also tested it seems, it might work.
f
I just pinged Erik, and asked him to publish his package, even if it's not completely finished yet.
e
It's Dart-compatible, which means it's JavaScript-compatible.
ES5 version.
The documentation is a bit lacking, but see the test.
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\$"
h
I'll give it a go, thanks guys!