本文介绍了使用正则表达式查找/替换 NSString 中的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!
问题描述
我想使用正则表达式来查找正则表达式模式的每个实例,即&*; 在我的字符串中并从中删除它,因此返回值是没有任何匹配项的原始字符串.也想使用相同的功能来匹配单词之间的多个空格,并改为使用单个空格.找不到这样的功能.
I would like to use regular expression to find every instances of a regular expression pattern I.e. &*; in my string and remove that from so the return value is the original string without any of the matches. Also would like to use the same function to match multiple spaces between words and have a single space instead. Could not find such a function.
示例输入字符串
NSString *str = @"123 &1245; Ross Test 12";
返回值应该是
123 Ross Test 12
如果有任何匹配此模式 "&* 或多个空格并将其替换为 @"";
If anything matching this pattern "&* or multiple white spaces and replaces it with @"";
推荐答案
NSString *string = @"123 &1245; Ross Test 12";
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"&[^;]*;" options:NSRegularExpressionCaseInsensitive error:&error];
NSString *modifiedString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@""];
NSLog(@"%@", modifiedString);
这篇关于使用正则表达式查找/替换 NSString 中的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
The End


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)