Reputation: 251
I have a sample of tex just like this.I need objective c to find for example string "龜" and return all containing line
齶 737B U9f76 B211 S24 XJ15642 N5437 V7100 MN48725 MP12.1108 P1-15-9 I3d21.1 Q2672.7 Ye4 Wag ガク あご はぐき {jaw}
龕 737C U9f95 B9 C212 S22 N568 V7104 MN48839 MP12.1151 P2-2-20 I2a20.3 Q8021.1 Ykan1 Wgam カン ガン れい {alcove for an image}
龜 737D U9f9c B213 S18 S16 XJ03535 XH2147 XN5445 V7106 H2150 O2598A MN48847 MP12.1151 P2-2-16 I2n9.1 Q2711.7 ZPP4-18-4 Ygui1 Yjun1 Yqiu1 Wgwi Wgu Wgyun キ キュウ キン かめ T1 ひさし {turtle} {tortoise}
龠 737E U9fa0 B214 S17 N5446 V7107 H2149 MN48882 MP12.1159 P2-2-15 I2a15.5 Q8022.7 ZPP2-3-14 Yyue4 Wyag ヤク ふえ {flute}
Upvotes: 1
Views: 67
Reputation: 437432
You can use NSRegularExpression
in with the NSRegularExpressionAnchorsMatchLines
option (so that ^
and $
will match the start and end of a line). You could use capturing parentheses, too, if you wanted, but they're not needed here.
Thus:
NSString *fullContent = @"齶 737B U9f76 B211 S24 XJ15642 N5437 V7100 MN48725 MP12.1108 P1-15-9 I3d21.1 Q2672.7 Ye4 Wag ガク あご はぐき {jaw}\n"
"龕 737C U9f95 B9 C212 S22 N568 V7104 MN48839 MP12.1151 P2-2-20 I2a20.3 Q8021.1 Ykan1 Wgam カン ガン れい {alcove for an image}\n"
"龜 737D U9f9c B213 S18 S16 XJ03535 XH2147 XN5445 V7106 H2150 O2598A MN48847 MP12.1151 P2-2-16 I2n9.1 Q2711.7 ZPP4-18-4 Ygui1 Yjun1 Yqiu1 Wgwi Wgu Wgyun キ キュウ キン かめ T1 ひさし {turtle} {tortoise}\n"
"龠 737E U9fa0 B214 S17 N5446 V7107 H2149 MN48882 MP12.1159 P2-2-15 I2a15.5 Q8022.7 ZPP2-3-14 Yyue4 Wyag ヤク ふえ {flute}";
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^.*?龜.*?$" options:NSRegularExpressionAnchorsMatchLines error:&error];
NSTextCheckingResult *match = [regex firstMatchInString:fullContent options:0 range:NSMakeRange(0, [fullContent length])];
if (match) {
NSString *matchString = [fullContent substringWithRange:match.range];
NSLog(@"%@", matchString);
}
The regex string ^.*?龜.*?$
translates to "find any line that contains this kanji character (龜) preceded by or followed by any sequence of characters." The ^
matches the start of the line (because of the NSRegularExpressionAnchorsMatchLines
option). The $
matches the end of the line. The .*?
sequences match the shortest possible string of characters necessary to satisfy the rest of the regex.
Upvotes: 1
Reputation: 17637
You could try something like this (not tested):
NSString *testStr = @"齶 737B U9f76 B211 S24 XJ15642 N5437 V7100 MN48725 MP12.1108 P1-15-9 I3d21.1 Q2672.7 Ye4 Wag ガク あご はぐき {jaw}
龕 737C U9f95 B9 C212 S22 N568 V7104 MN48839 MP12.1151 P2-2-20 I2a20.3 Q8021.1 Ykan1 Wgam カン ガン れい {alcove for an image}
龜 737D U9f9c B213 S18 S16 XJ03535 XH2147 XN5445 V7106 H2150 O2598A MN48847 MP12.1151 P2-2-16 I2n9.1 Q2711.7 ZPP4-18-4 Ygui1 Yjun1 Yqiu1 Wgwi Wgu Wgyun キ キュウ キン かめ T1 ひさし {turtle} {tortoise}
龠 737E U9fa0 B214 S17 N5446 V7107 H2149 MN48882 MP12.1159 P2-2-15 I2a15.5 Q8022.7 ZPP2-3-14 Yyue4 Wyag ヤク ふえ {flute}";
NSRegularExpression *pRegex = [NSRegularExpression regularExpressionWithPattern:@"龜.*"
options:NSRegularExpressionCaseInsensitive error:&error];
[pRegex enumerateMatchesInString:testStr options:0 range:NSMakeRange(0, [testStr length])
usingBlock:^(NSTextCheckingResult *matches, NSMatchingFlags flags, BOOL *stop){
// Do something with the matches found
}];
Upvotes: 0