• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Regular Expressions?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Regular Expressions?


  • Subject: Re: Regular Expressions?
  • From: John Engelhart <email@hidden>
  • Date: Sat, 7 Jun 2008 13:07:56 -0400


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:
This email sent to email@hidden


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

  • Prev by Date: Re: 3rd Party Nonsense (was Re: Regular Expressions?)
  • Next by Date: Re: Finding front view in a Cocoa app
  • Previous by thread: Re: Regular Expressions?
  • Next by thread: Re: List Guidelines and Resources
  • Index(es):
    • Date
    • Thread