Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add =~ operator for regex check #14

Closed
fredpi opened this issue Feb 22, 2018 · 7 comments · Fixed by #18
Closed

Add =~ operator for regex check #14

fredpi opened this issue Feb 22, 2018 · 7 comments · Fixed by #18

Comments

@fredpi
Copy link
Contributor

fredpi commented Feb 22, 2018

I suggest to add a new infix operator =~ for checking whether a string matches a regex using the following implementation:

infix operator =~
func =~ (lhs: String, rhs: String) -> Bool {
    return lhs.range(of: rhs, options: .regularExpression) == lhs.range(of: lhs)
}

Use:

let stringMatchesRegex = "ABC" =~ "[A-Z]+"

@Jeehut
Copy link
Member

Jeehut commented Feb 23, 2018

I must say, the usage example looks really weird to me. If not documented that way, I'm not against adding such an operator. It's used in many other languages, so I think having an operator here is fine.

Note though, that I would also find it interesting to add support for getting captures easily. Until now I always used https://github.com/sharplet/Regex which I think both reads great and is a breeze to write.

@Jeehut
Copy link
Member

Jeehut commented Feb 23, 2018

Would you be up for a PR?

@fredpi
Copy link
Contributor Author

fredpi commented Feb 24, 2018

Why's the usage example weird? Not such a common use, but very basic ;)

Captures would require more than just adding a single operator, so I would suggest only adding the operator for the initial PR, alright?

@Jeehut
Copy link
Member

Jeehut commented Feb 26, 2018

It's weird because it reads like this for an unexperienced developer:
"let stringMatchesRegex equal "ABC" equal (again?) "[A-~]+" ... WTF?" 😄

So for documentation, please use a more common usage example like this:

if "ABC" =~ "[A-Z]+" {
    // do something
}

One thing I wonder is, will Xcode give you Regex errors if you use the infix operator? I don't think so. Having used sharplet/Regex I know that it's working there which adds a lot of safety to defining Regex strings. Do you think, you can use an implementation that fixes that?

Regarding letting go of the captures: Sure, let's start with one feature and add others later on. What I was trying to say is: Wouldn't it go against one of the major three goals of Swift – namely being Expressive – to use an operator? Shouldn't we be using something like sharplet/Regex does, a type for the regular expression with lots of handy features?

I'm not entirely against this, but I'm not completely convinced it's a good addition either.

@fredpi
Copy link
Contributor Author

fredpi commented Feb 26, 2018

You're probably right about the operator not really fitting into the Swift language. There's a reason operators such as ++ where removed.

With this in mind, I would actually withdraw my proposal, but suggest adding some other way for better regex processing to HandySwift.

@Jeehut
Copy link
Member

Jeehut commented Feb 27, 2018

What do you think about copying the classes Regex, MatchResult and String+ReplaceMatching off from sharplet/Regex and cleaning them up to only include the core functionality? I feel like it's pretty Swifty and has a good coverage of use cases.

@fredpi
Copy link
Contributor Author

fredpi commented Feb 27, 2018

Seems like a good idea to include core regex functionality without blowing up this repo with too much regex interfaces. Will figure out what's needed imo and make a PR in the upcoming weeks.

@fredpi fredpi mentioned this issue Mar 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants