How do you use NSRegularExpression's replacementStringForResult:inString:offset:template:

前端 未结 6 950
清酒与你
清酒与你 2020-12-31 06:30

I have a series of characters that I want to match with a regular expression, and replace them with specific strings depending on what they are.

Example:

In

6条回答
  •  暖寄归人
    2020-12-31 07:21

    You should use

    - (NSString *)stringByReplacingMatchesInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range withTemplate:(NSString *)template
    

    or

    - (NSUInteger)replaceMatchesInString:(NSMutableString *)string options:(NSMatchingOptions)options range:(NSRange)range withTemplate:(NSString *)template
    

    with string being "This is the input string where i want to replace 1 2 & 3" and template being either "ONE", "TWO" or "THREE".

提交回复
热议问题