2011-09-24 70 views
3

我想解析使用-componentsSeparatedByString:以下字符串:如何使用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作爲字符串通過分離,子章節元素也得到分解,因爲它們包含在其中的篇章。

如何避免在使用-componentsSeparatedByString解析章節時停止SUBCHAPTER元素:?

回答

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); 
相關問題