IOS Regular Expressions Remove special symbols

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.