-
-
Notifications
You must be signed in to change notification settings - Fork 28
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
Comments
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. |
Would you be up for a PR? |
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? |
It's weird because it reads like this for an unexperienced developer: 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. |
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. |
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. |
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. |
I suggest to add a new infix operator
=~
for checking whether a string matches a regex using the following implementation:Use:
let stringMatchesRegex = "ABC" =~ "[A-Z]+"
The text was updated successfully, but these errors were encountered: