Regular Expression splitting up of NSString - iOS

南楼画角 提交于 2019-12-19 04:14:10

问题


Split String using Regular Expression in iOS

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

My original string could be like the following

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

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

NSString *originalStringVerThree = @"345 Mrs Someone";

I need to separate into 3 separate new strings:

  • Number with or with a trailing "a" or "b", remove white space between if exists
  • Person name, maybe capital or not, ie mr smith or Mrs Jones etc
  • After this, zero or more words to be in the final string

For example

  • 123a mr who here are some words
  • 124 b mrs jones n/p
  • 654 Mr Foo
  • 123 Jones n/p
  • 345 n/p

Should result in the following

line 1

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

line 2

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

line 3

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

line 4

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

line 5

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

The constants will be

  1. 3 digit number with or without an "a" "b" (123, 123 a, 123b)
  2. A persons name, with or without salutation (Mr jones, jones)
  3. The persons name could be unknown - hence the exact text of "n/p"
  4. Following the name is a string of n length that will end with a \n (this is a set of words\n).

The removal of the white space from 123 a into 123a is ideal but not major requirement


回答1:


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

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];

Make a test array:

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"
                       ];

Process the test array:

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");
    }
}

Output:

    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


来源:https://stackoverflow.com/questions/18799566/regular-expression-splitting-up-of-nsstring-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!