-->

Replacing in NSString with wildcards / regular exp

2020-02-02 03:40发布

问题:

NSString *x=@"\"/Sagar\' and \'Samir\' ";

Now, I want to remove characters between these.

\" /

\'

Intended output

x = (should have) @"and \'Samir\'"

So, Ms word give some options in find & replace, using wild card characters. ( just giving example )

Is it possible in cocoa?

回答1:

I'll assume you're looking for a solution in general -- not for this particular string. You'll want to learn about regular expressions.

For regular expression support in Objective-C check out RegexKitLite. It provides category methods on NSString that support various regex matching and substitution.



回答2:

You could also use NSScanner on this quite easily. A quick read through this will give you all you need to know:

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Strings/Articles/Scanners.html#//apple_ref/doc/uid/20000147-BCIEFGHC



回答3:

You could use straight C stuff. Here is a regex library allowing you to use NSString. This site has a small example of use.