sagarkothari
sagarkothari

Reputation: 24820

Replacing in NSString with wildcards / regular expressions - cocoa

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?

Upvotes: 0

Views: 8831

Answers (4)

Motti Shneor
Motti Shneor

Reputation: 2194

As all previous answers point you to the general concept of regular expressions, I thought to be more specific.

There are, of course, many options for doing this, both in th C, Obj-C and Cocoa Framework levels, But I'll present two.

The simplest wild-card matching for NSString can be found in an NSObject category defined in #import <Foundation/NSScriptWhoseTest.h> that provides many high-level comparison. Among them these two:

@interface NSObject (NSComparisonMethods)
  // argument should be a string using simple shell wildcards (* and ?). (e.g. "Stev*" or "N?XT"). Returns NO if receiver is not an NSString.
- (BOOL)isLike:(NSString *)object;
- (BOOL)isCaseInsensitiveLike:(NSString *)object;
@end

So,

if ([@"\"/Sagar\' and \'Samir\' " isLike:@"\"/*\' and \'Samir\' "]) {
    NSLog (@"Match!");
}

will log "Match!"

If you want the strongest tool, use NSRegularExpression either directly, or through its support from within NSString. using NSString, you can do this:

    NSMutableString *x= [[NSMutableString alloc] initWithString:@"\"/Sagar\' and \'Samir\' "];
NSLog (@"String: %@", x);
NSString *regex = @"\\\"[/a-zA-z]+\\\'";
NSString *replacement = @"";
NSLog (@"RegEx: %@ Replacement:%@", regex, replacement);

NSLog (@"RegEx: %@ Replacement:%@", regex, replacement);
NSRange allJson = NSMakeRange(0, json.length);

// Using NSString support for regular expressions
NSUInteger hits = [json replaceOccurrencesOfString:regex withString:replacement options:NSRegularExpressionSearch range:allJson];

// Using NSRegularExpression directly
NSError *error = nil;
NSRegularExpression *exp = [NSRegularExpression regularExpressionWithPattern:regex options:0  error:&error];
NSUInteger hits = [exp replaceMatchesInString:json options:0 range:allJson withTemplate:replacement];

NSLog (@"%lu hits in: %@", hits, x);

and your output will look like this:

2020-12-01 13:04:49.8908 Test[420:799] String: "/Sagar' and 'Samir'
2020-12-01 13:04:51.3608 Test[420:799] RegEx: \"[/a-zA-z]+\' Replacement:
2020-12-01 13:04:53.0415 Test[42570:724799] 1 hits in:  and 'Samir'

Upvotes: 0

sosborn
sosborn

Reputation: 14694

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

Upvotes: 3

nall
nall

Reputation: 16149

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.

Upvotes: 4

Donald Byrd
Donald Byrd

Reputation: 7788

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

Upvotes: 2

Related Questions