• 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: how to get this type of regex matching happening (using AGRegex)?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how to get this type of regex matching happening (using AGRegex)?


  • Subject: Re: how to get this type of regex matching happening (using AGRegex)?
  • From: Aram Greenman <email@hidden>
  • Date: Sat, 20 Mar 2004 10:24:17 -0800

AGRegex *regex = [[AGRegex alloc] initWithPattern:@"anchor
((\\w)\\s*)*"];

example input text 1:
anchor a b c d

hoped for array from example text 1:
a
b
c
d


Each set of parentheses only remembers the last occurrence of the subpattern. You'll probably have to use two regexes.

AGRegex *re1 = [AGRegex regexWithPattern:@"anchor (((\\w)\\s*)*)"];
NSString *s = [[re1 findInString:@"anchor a b c d"] groupAtIndex:1];

// s should now be "a b c d"

AGRegex *re2 = [AGRegex regexWithPattern:@"(\w)\\s*"];
NSArray *matches = [re2 findAllInString:s];

// matches is an array of AGRegexMatches
matches[0] = {"a ", "a"}
matches[1] = {"b ", "b"}
matches[2] = {"c ", "c"}
matches[3] = {"d", "d"}


I'm guessing this is probably a simplified example of what you really want to do, but if it's not you can do this a lot easier with NSScanner:

NSScanner *scanner = [NSScanner scannerWithString:@"anchor a b c d"];
NSString *match;

[scanner scanString:@"anchor " intoString:NULL];

while ([scanner scanCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:&match])
NSLog(match);


Aram
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


  • Follow-Ups:
    • Re: how to get this type of regex matching happening (using AGRegex)?
      • From: Ben Dougall <email@hidden>
  • Prev by Date: Re: memory management question
  • Next by Date: Loading a default file in doc based app
  • Previous by thread: Re: how to get this type of regex matching happening (using AGRegex)?
  • Next by thread: Re: how to get this type of regex matching happening (using AGRegex)?
  • Index(es):
    • Date
    • Thread