This is a bit of a n00bish question… but if I’m tr...
# announcements
a
This is a bit of a n00bish question… but if I’m trying to match an email pattern with a regex… and I have some pattern “patern”.toRegex()… what is the proper way to find out if a string matches that regex? in my case I’m using the RFC email address regex to validate, but I’m getting false results when I do. Here’s the link to the regex
Copy code
val regex = "Regex here".toRegex()
regex.matches("<mailto:first.last@test.com|first.last@test.com>")
what am I missing?
r
I’d include the actual strings you’re using if I were you - high chance it’s the regex or the email address that are the issue.
👍 1
a
I just updated it
I thought I’d spare the channel because the regex is huge
lol
could it be something with regex defaulting to multi line/single line or something like that?
r
You could drop it in a gist or just in the thread.
a
good point
r
Did you develop it, or did it come from somewhere? You probably know this, but there are loads of flavours of regex, and that’s a pretty complicated one, lots of room for something to be wrong!
a
that’s a good point. I got it from a SO post… I just linked it
but you are most likely right. Probably not the same flavor of Regex
r
Apparently that’s a PERL regex - it wouldn’t be surprising if you can’t use it direct as a JVM regex.
a
if it where right that is how I should be implementing it right?
r
Yes, your kotlin is fine.
a
that’s what I was thinking too… and it’s odd because in intelliJ that regex check passes when I enter the email address I used… but in my actual test code it does not pass.
so odd.
I think there must have been an extra character in there… I just pasted it from a regex101.com and it worked. 🙌
I appreciate your help @Rob Elliot
r
a
just needed a sanity check. 😂
s
Perl regex what does it mean, you mean PCRE.
a
thank you man! I seriously appreciate it. I spent too long on that last night
s
also PCRE is then a regex engine, as BRE (POSIX) for instance
one has to be careful which regex dialect one uses.
r
s
clearly can’t just copy one regex from one to another regex engine
a
Thank you guys for your help. I really appreciate it. @Rob Elliot I’ll check out that post for sure. 👍
t
I know it is going off topic and it is overkilling, but I had the same conversation with a colleague a couple of weeks ago. Don’t use a regex, use a library. for validation I would go with hibernate validator, or https://github.com/rcapraro/kalidation seems a nice wrapper around hibernate to get more of a kotlin style
👍 1
s
@Alexander Black yeah it surprised me years ago too, the full RFC compatible regex is complex because address specification is complex too
a
@stephanmg You are telling me… 😂 that thing is crazy.
s
@thanksforallthefish yeah that’s what I wanted to suggest too. Don’t do regex for this IMHO (Also I’m a big times regex fan and advocate whenever possible) 🙂
@Alexander Black I personally love regex and like to read it
🙂
a
Awesome! I have never heard of that. I’ll check it out. I really appreciate the advice.
s
enjoy your journey! 😄
a
thank you. 🙂
t
tbh hibernate ends up in a regex, because that is how a valid email is defined in rfc, but at least you hide it 🙂
s
@thanksforallthefish yeah I don’t know the library, but this makes sense, right?
t
or, as the article said, just don’t do validation, the worst that can happen is the email is not sent (which happen as well with a valid but wrong email anyway)
a
@stephanmg @thanksforallthefish It’s actually really funny you suggest that library, because I was literally writing something very similar. I love Kotlin’s DSL style… this is the syntax I created last night as a test:
Copy code
val validate = Validate("aaaaaaaa") {
            +Max(30)
            +Min(3)
        }
s
well, I mean this seems obvious to do, right? to hide the implementation detail (regex) from the user
why should we bother
I’m not surprised somebody came up with this kind of library already 😄
a
totally. It’s the obvious syntax given the awesomeness of the language. but I might port that library, because I’m building a Kotlin Multi Platform app.
s
cool
what are you building?
@Alexander Black
a
@stephanmg I’m merging the code for an iOS and Android app that I manage at my company to make adding new features easier and keep the apps in better sync. I work for a book publisher called Blackstone.
I have been working exclusively on Android for maybe 6 years now… and just got pulled into the iOS side because our iOS dev got too busy with other projects, so I figured if I’m going to manage this… I should have one unified codebase for the core.
s
wow cool
sounds like an interesting project
a
ya it’s really fun actually. I’m loving the multiplatform setup. There are still a few pain points, but generally it’s not bad at all. What do you do?
t
@Alexander Black given how many interesting libraries don’t die in the end, maybe you could about contributing to one of the already existing kotlin libraries for validation rather than rolling out your own. this thing are fun, and it is cool to have alternatives, but it becomes a pain when your library of choice loses support 🙂
😂 1
a
@thanksforallthefish I think that’s a good point. Probably better to contribute to something existing. I definitely know the pain of finding new libraries that solve problems only to realize they are no longer supported.