簡體   English   中英

正則表達式拆分NSString - iOS

[英]Regular Expression splitting up of NSString - iOS

在iOS中使用正則表達式拆分字符串

我已經使用循環解決了這個問題,但是想要一個更清晰的答案,我希望一個reg exe大師可以幫助我。

我的原始字符串可能如下所示

NSString *originalString = @"343 a mr smith needs this work";

NSString *originalStringVerTwo = @"345a mr jones needs this work as well";

NSString *originalStringVerThree = @"345 Mrs Someone";

我需要分成3個單獨的新字符串:

  • 帶有或帶有尾隨“a”或“b”的數字,刪除if之間的空格
  • 人名,也許是資本與否,即史密斯先生或瓊斯夫人等
  • 在此之后,零個或多個單詞將出現在最終字符串中

例如

  • 123a先生,這里有些話
  • 124 b mrs jones n / p
  • 654 Foo先生
  • 123 Jones n / p
  • 345 n / p

應該導致以下結果

第1行

NSString *one = 123a
NSString *two = mr who
NSString *three = here are some words

第2行

NSString *one = 124b // i want the white space removed between number and digit
NSString *two = mrs jones
NSString *three = n/p

第3行

NSString *one = 654
NSString *two = Mr Foo
NSString *three = @""

第4行

NSString *one = 123
NSString *two = Jones
NSString *three = n/p

第5行

NSString *one = 345
NSString *two = n/p
NSString *three = @""

常數將是

  1. 帶或不帶“a”“b”的3位數字(123,123a,123b)
  2. 一個人的名字,有或沒有稱呼(瓊斯先生,瓊斯先生)
  3. 人名可能是未知的 - 因此“n / p”的確切文本
  4. 名稱后面是一個n長度的字符串,以\\ n結尾(這是一組單詞\\ n)。

將123a的空白區域移除到123a是理想的,但不是主要要求

這是一個應該有效的正則表達式:

       ^             //start of line
       (             //first capture group
            [\d]+    //one or more digits
       )             //end of first capture group 

       (?:           //start of optional non-capturing group
              \s?    //optional whitespace
            (        //second capture group
              [ab]   //character class - a or b
            )        //end of second capture group 
       )?            //end of optional non-capturing group 

       \s            //whitespace

       (             //third capture group
            (?:      //non-capturing group
      Mr|Mrs|Mister  //title alternation
            )         
            \s       //whitespace
            [\w/]+   //1 or more word characters or "/"
       |             //alternation 
            [\w/]+   //1 or more word characters or "/"
       )             //end of third capture group 

       (?:           //start of optional non-capturing group  
            \s       //whitespace
            (        //fourth capture group
            .*       //0 or more of any character
            )        //end of fourth capture group
        )?           //end of optional non-capturing group
       $             //end of line

構建你的正則表達式。 我們必須逃避轉義以將它們保留在NSString中:

NSString* regexString =
@"^([\\d]+(?:\\s?[ab])?)\\s((?:Mr|Ms|Mrs|Mister)\\s[\\w/]+|[\\w/]+)(?:\\s(.*))?$";

NSRegularExpression *regex =
[NSRegularExpression regularExpressionWithPattern:regexString
                     options:NSRegularExpressionCaseInsensitive
                     error:nil];

制作測試數組:

NSArray* testArray = @[
                        @"123a mr who here are some words"
                       ,@"124 b mrs jones n/p"
                       ,@"654 Mr Foo"
                       ,@"123 Jones n/p"
                       ,@"345 n/p"
                       ,@"345"
                       ,@"nothing here"
                       ];

處理測試數組:

for (NSString* string in testArray) {
    NSLog(@" ");
    NSLog(@"input: '%@'",string);

    NSRange range = NSMakeRange(0,string.length);
    if ([regex numberOfMatchesInString:string options:0 range:range] == 1) {
        NSString* body = [regex stringByReplacingMatchesInString:string
                                           options:0
                                             range:range
                                      withTemplate:@"$1\n$2\n$3"];


        NSArray* result = [body componentsSeparatedByString:@"\n"];
        NSString* one = result[0];
        NSString* two = result[1];
        NSString* three = result[2];
        NSLog(@"one:   '%@'",one);
        NSLog(@"two:   '%@'",two);
        NSLog(@"three: '%@'",three);
    } else {
        NSLog(@"no match");
    }
}

輸出:

    input: '123a mr who here are some words'
    one:   '123a'
    two:   'mr who'
    three: 'here are some words'

    input: '124 b mrs jones n/p'
    one:   '124b'
    two:   'mrs jones'
    three: 'n/p'

    input: '654 Mr Foo'
    one:   '654'
    two:   'Mr Foo'
    three: ''

    input: '123 Jones n/p'
    one:   '123'
    two:   'Jones'
    three: 'n/p'

    input: '345 n/p'
    one:   '345'
    two:   'n/p'
    three: ''

    input: '345'
    no match

    input: 'nothing here'
    no match

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM