Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Regular Expressions?




On Jun 6, 2008, at 1:27 PM, Vincent E. wrote:

When I mentioned "perl -pe 's/\b(.*?)/\u\L$1/g'" I actually wasn't asking for any ObjC method with a look-alike syntax.
I actually wouldn't give a damn about "how" ("s///g") to pass a regex pattern to a method. ;)


I was rather asking whether RegExKit (or even RegExKitLite?) would generally be able to perform RegEx driven string replacements
where the replacement string contains stuff like "match back- references" (\1, \2, \<named>, …) or string modificators like "\L, \U".


Now to answer my own question:

RegExKit has this function which (according to the documentation) seems to do just what I was looking for:
[subjectString stringByMatching:regexString withReferenceString:templateString];


And for the latter (\L, \U, etc) I unfortunately had to find this in the PCRE documentation:
5. The following Perl escape sequences are not supported: \l, \u, \L,
\U, and \N. In fact these are implemented by Perl's general string-han-
dling and are not part of its pattern matching engine. If any of these
are encountered by PCRE, an error is generated.
http://www.pcre.org/pcre.txt


Thanks a lot any way.
Vincent

Actually, RegexKit handles perl like \u \l \U \L \E case conversions.

http://regexkit.sourceforge.net/Documentation/NSString.html#ExpansionofCaptureSubpatternMatchReferencesinStrings

example = [NSString stringWithUTF8String:"Stra\xc3\x9f" "e"]; // Straße
upper = [example stringByMatching:@"(.*)" replace:RKReplaceAll withReferenceString:@"\\U$1\\E"]; // STRASSE
lower = [upper stringByMatching:@"(.*)" replace:RKReplaceAll withReferenceString:@"\\L$1\\E"]; // strasse != Straße


As the example shows, case conversion is Unicode aware as well (ß -> SS -> ss). Your example would become:

[@"Some string to do work on" stringByMatching:@"\\b(.*?)" replace:RKReplaceAll withReferenceString:@"\\u\\L$1"];

The replace:RKReplaceAll is the functional equivalent of the /g modifier._______________________________________________

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:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >Re: Regular Expressions? (From: David Hoerl <email@hidden>)
 >Re: Regular Expressions? (From: "Vincent E." <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.