0

I would like to make my life translating Objective-C to Swift easier; I'm currently writing a program that will match certain often-used constructions (such as methods, properties and variables) and replace them with the Swift version. Now I'm trying to replace the +/- in front of a method name optionally to either class if + or nothing if -.

I can't seem to find how I can make the alternation (\+|-) be replaced with either class or based on the choice. Is this possible in pure regex? Or do I need to put this in my application logic?

Edit: expected input and output:

- (void)composeView(...) gives func composeView(...)

+ (void)composeView(...) gives class func composeView(...)

vrwim
  • 13,020
  • 13
  • 63
  • 118
  • can you put an input sample and expected output? – Federico Piazza Mar 09 '15 at 19:53
  • Downvoter, care to explain? – vrwim Mar 23 '15 at 18:55
  • I haven't downvoted but I think the downvote might be related to the lack of research for your question. Community usually gets mad when questions don't demonstrate any level of research. – Federico Piazza Mar 23 '15 at 21:02
  • It doesn't really bother me, it bothers me that someone downvoted me without leaving a comment why. This helps nobody. And I did research, I just wanted a feature that didn't exist, which is why I asked if I might be looking with the wrong search terms. – vrwim Mar 23 '15 at 21:04
  • well, I understand, same happened to me many times with the answers. Did it :) – Federico Piazza Mar 23 '15 at 21:08

1 Answers1

0

You can use two regex, the first one can be:

- \(\w+\)(composeView\(.*?\))

Working demo

Take a look at the Substitution section

enter image description here

And the second one:

\+ \(\w+\)(composeView\(.*?\))

Working demo

enter image description here

Federico Piazza
  • 30,085
  • 15
  • 87
  • 123
  • So you're saying that something like an `inline if` is not possible in regex? If so, this is probably the best possible answer. – vrwim Mar 09 '15 at 20:31
  • @vrwim you can do `if` with regex but you can't decide what to replace. So, the best thing is two regex... is simpler and easier – Federico Piazza Mar 09 '15 at 20:48
  • Could you point me in the right direction for search terms on this regex `if`? – vrwim Mar 09 '15 at 21:04
  • @vrwim sure, you can take a look at this link http://www.regular-expressions.info/conditional.html – Federico Piazza Mar 09 '15 at 21:21