1, it is well known that the regular expression if used properly, its practicality is quite strong, basically all the pre-research is to support the regular expression, but the support of the grammar is different.
2, in the actual application, we may need to intercept the first special characters encountered before the text, at this time need to use the regular expression, after all, the special symbol so many, if someone really use multiple if to operate, can only say refreshed my cognition!
Not much to say, enclose the code
-(NSString *) Getnewsubstorenamebyregularexpression: (NSString *) storename{ nserror *error = NULL; Nsregularexpression *regex = [Nsregularexpression regularexpressionwithpattern:@ "[-() ()-" "$&@%^*?+?=|{}? 【】??? ¥!!.<>/:;:;,,,. ] "Options:nsregularexpressioncaseinsensitive | nsregularexpressiondotmatcheslineseparators | Nsregularexpressionanchorsmatchlines | Nsregularexpressionallowcommentsandwhitespace error:&error]; NSString *result = [Regex stringbyreplacingmatchesinstring:storename options:0 range:nsmakerange (0, [storeName length] ) withtemplate:@ "PK"]; return [result componentsseparatedbystring:@ "PK"].firstobject;}
The above code means to first replace the special characters with PK, then use the PK as the keyword to split, and take the first element.
Careful little partner will find that the special characters do not contain the "#" character,"# " as a character contained in the regular expression, will cause the entire regular expression validation failure, this conclusion or I found by the dichotomy method. Therefore, "#" is removed from the expression
IOS Regular Expressions Remove special symbols