Re: NSRegularExpression only finds a single occurrence of my match
Re: NSRegularExpression only finds a single occurrence of my match
- Subject: Re: NSRegularExpression only finds a single occurrence of my match
- From: Roland King <email@hidden>
- Date: Sun, 17 Oct 2010 15:36:43 +0800
Several things here. Firstly your regular expression matches the whole string because you have a '*' after the capture group. Putting a '*' after a capture group doesn't make the RE match multiple times and make multiple captures, it just makes that group repeat and the capture group will end up with the last value captured.
eg in your case below, if you'd looked at the result of the regular expression match instead of just the count, you'd see it's captured the entire string, has two ranges, the first of which is the whole string and the second of which is the last 4 characters "Home", the matching is like this
number -> matches [a-z]+
Of -> matches [A-Z][a-z]+ and captures into \1 and repeats because of the *
Elements -> matches [A-Z][a-z]+ and captures into \1 and repeats because of the *
At -> matches [A-Z][a-z]+ and captures into \1 and repeats because of the *
Home -> matches [A-Z][a-z]+ and captures into \1
The whole string is matched and the result will contain 'Home' as the last value captured in that one capture group.
You just want the RegEx to be
[A-Z][a-z]+
Which will capture each string starting with a capital letter followed by small letters. That you'll find captures 4 times on your string.
Couple of other things, what's all that case insensitive stuff doing in there? Because you are specifying a character class it doesn't matter but it's misleading as you are trying to be case sensitive.
Your idea for an expression will fail on something like this numberOfPeasInAPod
which I assume you would want to change to number_of_peas_in_a_pod, because there are no small letters following the 'A', so a modificiation to the RE I suggested above would be
[A-Z][a-z]*
which that works for the above case.
Finally why are you using a Regular Expression for this at all, it's a bit heavyweight isn't it? You could use normal NSString methods to go find capital letters and then just replace the capital letter with _<lowercase version>
On 17-Oct-2010, at 2:38 PM, Devraj Mukherjee wrote:
> Hi all,
>
> I am trying to use Regular Expressions to match uppercase letters in
> strings like "numberOfNames" which I want to replace with
> number_of_names. Code sample
>
>
> NSError *error = NULL;
> NSRegularExpression *regex = [NSRegularExpression
> regularExpressionWithPattern:@"[a-z]+([A-Z][a-z]+)*"
> options:NSRegularExpressionAllowCommentsAndWhitespace
> error:&error];
>
>
> NSMutableString *aStr = [NSMutableString
> stringWithString:@"numberOfElementsAtHome"];
> NSLog(@"%i", [regex numberOfMatchesInString:aStr
> options:NSRegularExpressionCaseInsensitive
> range:NSMakeRange(0, [aStr length])]);
>
> In all cases my regular expressions matches one occurrence.
>
> Can anyone see what I might be getting wrong?
>
> May be I am using parameters in the regex string that
> NSRegularExpression doesn't support?
>
> Thanks a lot.
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden