-3

What would be a regular expression for &Anyword=

I need find the occurrence of this expression and replace it with some other template but i need to retain "Anyword" between "&" and "=".

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
chandvoid
  • 133
  • 1
  • 12
  • you can use `&Anyword=` as your regex, and then just replace it with what ever fits your needs – Argent May 19 '14 at 05:52

2 Answers2

1

If you want to find what occurs between the & and the =, you can use a regex string like @"&([^\\=]*)=" (i.e. "the characters between & and = which are, themselves, not the = character"). If you also wanted to capture not only &Anyword=, but also ?Anyword=, too, then you might use @"[&?]([^\\=]*)=".

When you find a match, you can use the NSRange to both identify what the old string was, as well as replace it with something else. For example:

For example, create the regex:

NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"&([^\\=]*)=" options:0 error:&error];
NSAssert(regex, @"Regex failed: %@", error);

Now use it to find the string between the & and the =:

NSString *string = @"http://www.apple.com/?device=iPhone&Anyword=true";

[regex enumerateMatchesInString:string options:0 range:NSMakeRange(0, [string length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
    NSRange range = [result rangeAtIndex:1];
    NSLog(@"range = %@", NSStringFromRange(range));

    NSString *foundKey = [string substringWithRange:range];
    NSLog(@"foundKey = %@", foundKey);

    NSString *newString = [string stringByReplacingCharactersInRange:range withString:@"os"];
    NSLog(@"newString = %@", newString);
}];

That reports:

range = {36, 7}
foundKey = Anyword
newString = http://www.apple.com/?device=iPhone&os=true

If you only want to find the first one, you can use firstMatchInString rather than going through all of them with enumerateMatchesInString, but hopefully this illustrates the idea.


If this was a URL, though, I might be inclined to get the query portion by just using the query method, and then I'd get the various key=value components with componentsSeparatedByString, and then extract both the key and the value out with another componentsSeparatedByString:

NSString *string = @"http://www.apple.com/?device=iPhone&Anyword=true";
NSURL *url = [NSURL URLWithString:string];
NSString *query = [url query];
NSLog(@"query = %@", query);
NSArray *parameterArray = [query componentsSeparatedByString:@"&"];
NSMutableDictionary *parameterDictionary = [NSMutableDictionary dictionary];
for (NSString *parameter in parameterArray) {
    NSArray *parameterComponents = [parameter componentsSeparatedByString:@"="];
    NSString *key = parameterComponents[0];
    NSString *value = parameterComponents[1];

    parameterDictionary[key] = value;
}

NSLog(@"parameterDictionary = %@", parameterDictionary);

That would yield:

query = device=iPhone&Anyword=true
parameterDictionary = {
    Anyword = true;
    device = iPhone;
}

You could then modify that dictionary as you saw fit, and then rebuild the URL, if you needed.

As you can see, you can approach a problem like this using regex, as shown earlier, or by exploding strings into arrays with componentsSeparatedByString (and if you want to join them back into strings, you can use componentsJoinedByString).

Rob
  • 415,655
  • 72
  • 787
  • 1,044
0

This should work out i believe.

    NSError *error;
    NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:@"&Anyword=" options:NSRegularExpressionIgnoreMetacharacters error:&error];
    NSString *reqdString = @"Some string with &Anyword= &Anyword= &Anyword= in it";

    //For a single match
    if ([regex firstMatchInString:reqdString options:NSMatchingReportCompletion range:NSMakeRange(0, reqdString.length)].resultType == NSTextCheckingTypeRegularExpression) {
        [reqdString stringByReplacingOccurrencesOfString:@"&Anyword=" withString:@"Replaceemt String"];
    }

    //For mutiple matches
    NSUInteger numberOfMatches = [regex numberOfMatchesInString:reqdString options:NSMatchingReportCompletion range:NSMakeRange(0, reqdString.length)];
    if (numberOfMatches) {
        for (int index = 0; index < numberOfMatches; index ++) {
            //Do your replacement stuff.
        }
    }

Hope this helps

Prathamesh Saraf
  • 709
  • 4
  • 14