정규 표현 식 패키지
:
xxxxxx[a:b]xxxxxx
:
xxxxxxaxxxxxx
:
xxxxxxbxxxxxx
:
:
3 [666: ]
:
3 666
:
3
:
:@"\[.?\]"
:@"\[.?\:"
: @"\:.*?\]"
[] :
---
#####.h
// Copyright © 2016 XDYB. All rights reserved.
//
import
@interface PatternString : NSObject
/**
*
* : [666: ] , 666,
*/
+(NSMutableArray *)PatternString:(NSString *)string;
/**
* :
* :
* :
* :
*/
- (NSMutableArray
*)matchesInStringAllFit:(NSString *)string entirePattern:(NSString *)entirePattern firstPattern:(NSString *)firstPattern lastPattern:(NSString *)lastPattern;
/**
*
*/
- (BOOL)isPatternString:(NSString *)string;
/**
*
*/
- (BOOL)formerlyString:(NSString *)string regularExpressionWithPattern:(NSString *)pattern;
@end
#####.m
// Copyright © 2016 XDYB. All rights reserved.
//
import "PatternString.h"
@implementation PatternString
//
+(NSMutableArray )PatternString:(NSString )string
{
NSMutableArray array = [PatternString matchesInStringAllFit:string entirePattern:@"\[.?\]" firstPattern:@"\[.?\:" lastPattern:@"\:.?\]"];
return array;
}
// ,
- (BOOL)isPatternString:(NSString )string
{
return [self formerlyString:string regularExpressionWithPattern:@"\[.?\]"];
}
// ,
-
(BOOL)formerlyString:(NSString *)string regularExpressionWithPattern:(NSString *)pattern{
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
//
NSArray *arr = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])];
return arr.count;
}
//
-
(NSMutableArray *)matchesInStringAllFit:(NSString *)string entirePattern:(NSString *)entirePattern firstPattern:(NSString *)firstPattern lastPattern:(NSString *)lastPattern
{
//
NSRange rangeStr;
//
NSTextCheckingResult * rsult;
//
NSMutableArray *array = [NSMutableArray arrayWithObject:string];
[array addObject:string];
//
//
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:entirePattern options:NSRegularExpressionCaseInsensitive error:nil];
//
NSArray *arr = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])];
//
NSUInteger count = arr.count;
int i = 2;
while (i > 0) {
i = i- 1;
while (count > 0) {
//
rsult = arr[--count];
rangeStr = rsult.range;
NSArray *arrayFirst = [self matchesInString:array.firstObject entirePattern:entirePattern firstPattern:firstPattern lastPattern:lastPattern];
[array replaceObjectAtIndex:0 withObject:arrayFirst.firstObject];
NSArray *arrayLast = [self matchesInString:array.lastObject entirePattern:entirePattern firstPattern:firstPattern lastPattern:lastPattern];
[array replaceObjectAtIndex:1 withObject:arrayLast.lastObject];
}
}
return array;
}
//
-
(NSArray *)matchesInString:(NSString *)string entirePattern:(NSString *)entirePattern firstPattern:(NSString *)firstPattern lastPattern:(NSString *)lastPattern
{
//
// 1 ------[]-------
// NSString pattern1 = @"\[.?\]";
NSString *pattern1 = entirePattern;
NSString *pattern1String = [self matchesInString:string regularExpressionWithPattern:entirePattern];
// 2 ------[:-------
// NSString pattern2 = @"\[.?\:";
NSString *pattern2String = [self matchesInString:pattern1String regularExpressionWithPattern:firstPattern];
// 3 ------:]-------
// NSString pattern3 = @"\:.?\]";
NSString *pattern3String = [self matchesInString:pattern1String regularExpressionWithPattern:lastPattern];
// 1
NSString *patternHeadStr = [self stringByReplacingString:pattern2String withReplacingFirstPatternString:@"" withReplacingLastPatternString:@""];
// 2
NSString *patternTailStr = [self stringByReplacingString:pattern3String withReplacingFirstPatternString:@"" withReplacingLastPatternString:@""];
if(patternHeadStr == nil || patternTailStr == nil)// :
{
return @[string,string];
}
//
NSString *finallyHeadString = [self matchesInString:string regularExpressionWithPattern:pattern1 stringByReplacingOccurrencesWithString:patternHeadStr];
NSString *finallyTailString = [self matchesInString:string regularExpressionWithPattern:pattern1 stringByReplacingOccurrencesWithString:patternTailStr];
return @[finallyHeadString,finallyTailString];
}
//
- (NSString *)stringByReplacingString:(NSString *)str withReplacingFirstPatternString:(NSString *)firstReplacement withReplacingLastPatternString:(NSString *)lastReplacement
{
NSRange range = NSMakeRange(0, 1);
str = [str stringByReplacingCharactersInRange:range withString:firstReplacement];
NSRange range1 = NSMakeRange(str.length - 1, 1);
str = [str stringByReplacingCharactersInRange:range1 withString:lastReplacement];
return str;
}
//
-
(NSString *)matchesInString:(NSString *)string regularExpressionWithPattern:pattern
{
//
NSRange rangeStr;
//
NSString *matchesInStr;
//
NSTextCheckingResult * rsult;
//
//
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
//
NSArray *arr = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])];
//
NSUInteger count = arr.count;
while (count > 0) {
//
rsult = arr[--count];
rangeStr = rsult.range;
//
matchesInStr = [string substringWithRange:rangeStr];;
}
return matchesInStr;
}
//
-
(NSString *)matchesInString:(NSString *)string regularExpressionWithPattern:(NSString *)pattern stringByReplacingOccurrencesWithString:(NSString *)replacement
{
NSString *replacingStr;
//
NSRange rangeStr;
//
NSString *matchesInStr;
//
NSTextCheckingResult * rsult;
//
//
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
//
NSArray *arr = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])];
//
NSUInteger count = arr.count;
while (count > 0) {
//
rsult = arr[--count];
rangeStr = rsult.range;
//
matchesInStr = [string substringWithRange:rangeStr];
// X
replacingStr = [string stringByReplacingOccurrencesOfString:matchesInStr withString:replacement];
}
return replacingStr;
}
@end
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.