本文介紹了使用正則表達式查找/替換 NSString 中的子字符串的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我想使用正則表達式來查找正則表達式模式的每個實例,即&*;
在我的字符串中并從中刪除它,因此返回值是沒有任何匹配項的原始字符串.也想使用相同的功能來匹配單詞之間的多個空格,并改為使用單個空格.找不到這樣的功能.
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模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!