1

I have this String:

{{nat fs g player|no=1|pos=GK|name=[[Hugo Lloris]]|age={{Birth date and age|1986|12|26|df=y}}|caps=73|goals=0|club=[[Tottenham Hotspur F.C.|Tottenham Hotspur]]|clubnat=ENG}}

and i want to get the data from this string so i build regex with:

https://regex101.com/r/jA1zS4/1

But when i run this code in the my project i get error:

NSString *string = @"{{nat fs g player|no=1|pos=GK|name=[[Hugo Lloris]]|age={{Birth date and age|1986|12|26|df=y}}|caps=73|goals=0|club=[[Tottenham Hotspur F.C.|Tottenham Hotspur]]|clubnat=ENG}}";
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"{{nat fs g player\\|no=(.*)\\|pos=(.*?)\\|name=\[\[(.*?)\\]\\]\\|age=\{\{Birth date and age\\|(.*?)\\|(.*?)\\|(.*?)\\|df=y\\}\\}\\|caps=(.*?)\\|goals=(.*?)\\|club=\[\[(.*?)\\|(.*)"
                                                                       options:NSRegularExpressionCaseInsensitive
                                                                         error:&error];

NSArray *matches = [regex matchesInString:string
                                  options:0
                                    range:NSMakeRange(0, [string length])];


Error Domain=NSCocoaErrorDomain Code=2048 "The value “{{nat fs g player\|no=(.*)\|pos=(.*?)\|name=[[(.*?)\]\]\|age={{Birth date and age\|(.*?)\|(.*?)\|(.*?)\|df=y\}\}\|caps=(.*?)\|goals=(.*?)\|club=[[(.*?)\|(.*)” is invalid." UserInfo={NSInvalidValue={{nat fs g player\|no=(.*)\|pos=(.*?)\|name=[[(.*?)\]\]\|age={{Birth date and age\|(.*?)\|(.*?)\|(.*?)\|df=y\}\}\|caps=(.*?)\|goals=(.*?)\|club=[[(.*?)\|(.*)}
1
  • You failed to escape the square brackets properly. Commented Nov 4, 2015 at 13:39

1 Answer 1

1

I'd use a bit different regex: I'd replace all .*? with [^|]* for better performance:

\{\{nat fs g player\|no=([^|]*)\|pos=([^|]*)\|name=\[\[([^|]*)\]\]\|age=\{\{Birth date and age\|([^|]*)\|([^|]*)\|([^|]*)\|df=y}}\|caps=([^|]*)\|goals=([^|]*)\|club=\[\[([^|]*)\|(.*)

And in Objective-C, you need to escape all |, [, ], { and }:

NSString *pattern = @"\\{\\{nat fs g player\\|no=([^|]*)\\|pos=([^|]*)\\|name=\\[\\[([^|]*)\\]\\]\\|age=\\{\\{Birth date and age\\|([^|]*)\\|([^|]*)\\|([^|]*)\\|df=y\\}\\}\\|caps=([^|]*)\\|goals=([^|]*)\\|club=\\[\\[([^|]*)\\|(.*)";

See IDEONE demo proving there is a match.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.