繁体   English   中英

正则表达式的日期,并使用stringByReplacingMatchesInString替换其在字符串中的出现

[英]Regex for date and replace its occurrence in string using stringByReplacingMatchesInString

我需要一个这样的日期正则表达式,“ dd / mm / yyyy hh:mm:ss pm”,然后我想在某些NSString使用此正则表达式匹配项来分隔字符串组件。

例如,如果字符串是,

30/06/15 11:46:37 pm: Person1: Testing
30/06/15 11:46:40 pm: Person1: Hello...
30/06/15 11:46:52 pm: Person2: Hi 
30/06/15 11:47:06 pm: Person1: How are you doing

数组中的输出组件应该是

Person1: Testing
Person1: Hello...
Person2: Hi 
Person1: How are you doing

您可以提出任何更好的主意来解析包含上述聊天文本的字符串。

Try This,
NSString *string = @"30/06/15 11:46:37 pm: Person1: Testing 30/06/15 11:46:40 pm: Person1: Hello... 30/06/15 11:46:52 pm: Person2: Hi 30/06/15 11:47:06 pm: Person1: How are you doing";
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"([0-9]{1,2})+\/+([0-9]{1,2})+\/+([0-9]{1,2})+ +([0-9]{1,2})+:+([0-9]{1,2})+:+([0-9]{1,2}) +(am|pm): " options:NSRegularExpressionCaseInsensitive error:&error];
NSString *modifiedString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@""];
NSLog(@"%@", modifiedString);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM