정규 표현 식 패키지

8575 단어
기본 수 요 는 다음 과 같 습 니 다.


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

좋은 웹페이지 즐겨찾기