Splitting equation made of NSString with NSRegularExpression and finding left side of Equation <p>I have a string such as below in my application:</p> <pre><code> b=c+32 </code></pre> <p>I parse the string using below code snippet to split it to b,=,c,+,32. That works fine. However I need to know which variable is left side of the "=" operator. This variable always comes first in the expression. Is there a way in NSRegularExpressions to give me index of the results so I can compare them to see what's left side of "=". If there's a better match that would give me the first element I would settle for that too.</p> <p>Here's the code to split them.</p> <pre><code> NSString * textpattern = @"[A-Za-z]+"; NSString * numberpattern = @"[0-9]+"; NSString * opPattern = @"[-+/*]"; NSString * copPattern = @"="; NSRegularExpression * reg = [NSRegularExpression regularExpressionWithPattern: @"(\\d+\\.\\d+)|(\\d+)|([a-z])|([+-/*=///^])|([/(/)])" options:0 error:nil]; NSArray *matches = [ reg matchesInString: exp options: 0 range:NSMakeRange(0, [exp length])]; for (NSTextCheckingResult *match in matches) { NSString* text = [exp substringWithRange:[match range]]; NSMutableDictionary *term = [NSMutableDictionary new]; if ([text doesMatchRegStringExp:textpattern]) { NSLog(@"variable"); [term setObject:@"variable" forKey:@"name"]; } else if ([text doesMatchRegStringExp:numberpattern]) { NSLog(@"number"); [term setObject:@"number" forKey:@"name"]; } else if ([text doesMatchRegStringExp:opPattern]) { NSLog(@"op"); [term setObject:@"op" forKey:@"name"]; } else if ([text doesMatchRegStringExp:copPattern]) { NSLog(@"cop"); [term setObject:@"cop" forKey:@"name"]; } else { [term setObject:@"other" forKey:@"name"]; } } // NSString+RegExpExtensions.h #import &lt;Foundation/Foundation.h&gt; @interface NSString (RegExpExtensions) - (BOOL)doesMatchRegStringExp:(NSString *)string; @end #import "NSString+RegExpExtensions.h" @implementation NSString (RegExpExtensions) - (BOOL)doesMatchRegStringExp:(NSString *)string { NSPredicate *regExpPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", string]; return [regExpPredicate evaluateWithObject:self]; } @end </code></pre> <p>Thank you. </p>