NSString的正则表达式拆分-iOS [英] Regular Expression splitting up of NSString - iOS

查看:133
本文介绍了NSString的正则表达式拆分-iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS中使用正则表达式分割字符串

Split String using Regular Expression in iOS

我已经使用循环解决了此问题,但是希望得到一个更简洁的答案,我希望reg exe专家可以帮助我.

I have solved this problem using loops, however would like a cleaner answer and I hope a reg exe guru can help me out.

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

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个单独的新字符串:

I need to separate into 3 separate new strings:

  • 以数字结尾或以"a"或"b"结尾的数字,如果存在则删除空格
  • 人名,也许不是大写,例如史密斯先生或琼斯太太等
  • 此后,最终字符串中将包含零个或多个单词

例如

  • 123a先生,这里有几句话
  • 124 b琼斯夫人n/p
  • 654傅先生
  • 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"(123、123a,123b)的3位数字
  2. 带或不带称呼的人的名字(琼斯先生,琼斯先生)
  3. 人名可能未知-因此"n/p"的确切文本
  4. 名称后跟一个长度为n的字符串,以\ n(这是一组单词\ n)结尾.

将空白从123a移除为123a是理想的选择,但不是主要要求

推荐答案

下面是一个应该起作用的正则表达式:

Here's a regex that should work:

       ^             //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中:

Construct your regex. We have to escape the escapes to retain them in an 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

这篇关于NSString的正则表达式拆分-iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆