Use regular expression to find/replace substring in NSString(使用正则表达式查找/替换 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 中的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用正则表达式查找/替换 NSString 中的子字符串
基础教程推荐
- 在 Android 模拟器中激活网络位置提供程序? 2022-01-01
- 在 iOS8 中无法获得正确的键盘高度值 2022-01-01
- 突出显示朗读文本(在 iPhone 的故事书类型应用程序中) 2022-01-01
- 可可/目标C(OSX不是iOS)从子对象访问父对象 2022-01-01
- Android:STATE_SELECTED不工作 2022-01-01
- 新的@SystemApi 注解是什么意思,和@hide 有什么区别 2022-01-01
- 如何将多个组件添加到 PickerView? 2022-01-01
- 在 appComponent dagger 2 中动态添加测试模块? 2022-01-01
- - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view 如何工作 2022-01-01
- 我的 UIImageView 的任务 2022-01-01
