0

I'm making an app using Swift, but in it I want to use the Ruby regex engine. I thought maybe I could execute Ruby from Swift, but I didn't find a way to do that. I also looked if anybody had implemented the Ruby regex engine in Swift, but I don't think they have.

Any ideas on how I can do this?

Zac
  • 813
  • 10
  • 22
  • Are you just having difficulties with NSRegularExpression, or do you have a specific reason for wanting to use Ruby in this case? – Eric Aya Dec 05 '16 at 17:25
  • My app is a regex testing app, in which the user will input a regex. I want it to be for ruby regexes because imo they're more useful – Zac Dec 05 '16 at 17:26
  • 1
    Define "more useful". There are other regular expression libraries you can use and the one in Ruby isn't particularly exceptional but for the fact it's convenient to use within Ruby. – tadman Dec 05 '16 at 18:16
  • 4
    Indeed, [PCRE](http://www.pcre.org/), for example, is *very* similar to Oniguma (Ruby's Regexp engine) and widely used, which should make it easy to integrate with your project. FWIW Wikipedia has a great [Comparison of regular expression engines](https://en.wikipedia.org/wiki/Comparison_of_regular_expression_engines#Language_features) page. – Jordan Running Dec 05 '16 at 18:38
  • I don't know Swift but it seems like it's possible to run shell commands. So you could run a ruby script if you wanted. http://stackoverflow.com/questions/26971240/how-do-i-run-an-terminal-command-in-a-swift-script-e-g-xcodebuild – max pleaner Dec 05 '16 at 22:21
  • 1
    @maxpleaner Calling Ruby via shell for each regular expression would be cripplingly slow for even the most trivial of things. Jordan's suggestion of using PCRE, which is easily accommodated with Swift, is the best call here if the NSRegularExpression is inadequate. – tadman Dec 06 '16 at 01:03
  • 1
    @Jordan PCRE looks very good, thanks! I'm likely going to use it now. If you want to post it as an answer, I'll accept it. – Zac Dec 06 '16 at 11:40
  • Oh, and @tadman, by _more useful_ I mean that I like to use Ruby more than I do Swift, so for my usage I'd rather do a Ruby regex. – Zac Dec 06 '16 at 11:40
  • Regular expressions have a lot of commonality across different programming languages and libraries. If you're doing Swift programming don't stubbornly cling to the Ruby way of doing things: The *When In Rome* principle applies. Take what you've learned from Ruby and apply it to how you write Swift code, but don't forget to do things the Swift way as well, it has its own paradigms to adhere to. You'll likely learn things from Swift that will improve your Ruby code. – tadman Dec 06 '16 at 18:59

1 Answers1

0

Define "more useful". There are other regular expression libraries you can use and the one in Ruby isn't particularly exceptional but for the fact it's convenient to use within Ruby. – tadman

Indeed, PCRE, for example, is very similar to Oniguma (Ruby's Regexp engine) and widely used, which should make it easy to integrate with your project. FWIW Wikipedia has a great Comparison of regular expression engines page. – Jordan Running

Armali
  • 18,255
  • 14
  • 57
  • 171