我想使用正则表达式来查找正则表达式模式的每个实例,即&*; 在我的字符串中并从中删除它,因此返回值是没有任何匹配项的原始字符串.也想使用相同的功能来匹配单词之间的多个空格,并改为使用单个空格.找不到这样的功能.
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 中的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
在 EditText 中仅允许基于正则表达式的选定字符Allow only selected charcters based on regex in an EditText(在 EditText 中仅允许基于正则表达式的选定字符)
如何在 Android 中使用正则表达式How to use regular expression in Android(如何在 Android 中使用正则表达式)
在没有应用程序源代码的情况下使用 Instruments 测Using Instruments to test an iOS app without having source code to the application(在没有应用程序源代码的情况下使用 Instruments 测试
KIF:如何自动运行/压力测试 iOS 应用程序以找出罕KIF: How to auto-run/stress test an iOS app to find the cause of a rare UI bug?(KIF:如何自动运行/压力测试 iOS 应用程序以找出罕见 UI 错
验证 EditText 中的电子邮件Validate an email inside an EditText(验证 EditText 中的电子邮件)
无法更改 Xcode 4.5 中的目标成员身份可见性Can#39;t change target membership visibility in Xcode 4.5(无法更改 Xcode 4.5 中的目标成员身份可见性)