如何使用 ComponentsSeparatedByString: 忽略包含子字符串的单词?

发布于 2024-12-06 04:31:27 字数 778 浏览 0 评论 0原文

我想使用 -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 元素上停止?

I would like to parse the following string using -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

but when I use CHAPTER as the string to separate by, the SUBCHAPTER elements also get broken up because they contain CHAPTER within them.

How can I avoid stopping on the SUBCHAPTER elements when parsing the CHAPTER ones using -componentsSeparatedByString:?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抱猫软卧 2024-12-13 04:31:27

尝试使用正则表达式 regexKit 非常适合此目的。这将帮助您形成您正在寻找的确切模式......

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

Try using regular expressions regexKit is perfect for this. That will help you to form the exact pattern you are looking for...

NSString *regex      = @"^CHAPTER [0-9]";
NSPredicate *valtest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex]; 
ret                  = [valtest evaluateWithObject:yourText];
NSLog("Matches: %@", ret);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文