如何使用componentsSeparatedByString忽略包含子串的单词:?

时间:2011-09-24 07:24:09

标签: iphone objective-c ios nsstring

我想使用-componentsSeparatedByString:解析以下字符串:

     CHAPTER 5. RULE OF DECISION
 CHAPTER 6. GOVERNMENTAL EXEMPTION FROM BOND AND SECURITY REQUIREMENTS
 CHAPTER 7. LIABILITY OF COURT OFFICERS
     SUBCHAPTER A. LIABILITY OF OFFICER
     SUBCHAPTER B. LIABILITY OF ATTORNEY
     SUBCHAPTER C. SUIT ON OFFICIAL BONDS
 CHAPTER 8. STATE EXEMPTION FROM CERTAIN FEES: FEES PAID BY OPPOSING PARTY
 CHAPTER 9. FRIVOLOUS PLEADINGS AND CLAIMS
     SUBCHAPTER A. GENERAL PROVISIONS
     SUBCHAPTER B. SIGNING OF PLEADINGS
 CHAPTER 10. SANCTIONS FOR FRIVOLOUS PLEADINGS AND MOTIONS

但是当我使用CHAPTER作为要分隔的字符串时,SUBCHAPTER元素也会被分解,因为它们中包含CHAPTER。

使用-componentsSeparatedByString解析CHAPTER时,如何避免停止SUBCHAPTER元素:?

1 个答案:

答案 0 :(得分:1)

尝试使用regular expressions regexKit is perfect for this。这将帮助您形成您正在寻找的确切模式......

NSString *regex      = @"^CHAPTER [0-9]";
NSPredicate *valtest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex]; 
ret                  = [valtest evaluateWithObject:yourText];
NSLog("Matches: %@", ret);