3

I have a string like this:

NSString* msg = @"Hello this is a new message to @[123:Dr Zoidberg] and his nippers";

And I want to use -stringByReplacingMatchesInString:options:range:withTemplate: to convert this pattern to:

NSString* msg = @"Hello this is a new message to Dr Zoidberg and his nippers"; 

This is what I have so far:

NSString* msg = @"Hello this is a new message to @[123:Dr Zoidberg] and his nippers";
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern: @"????"
                                                                       options: NSRegularExpressionCaseInsensitive
                                                                         error: nil];

NSString* plainText = [regex stringByReplacingMatchesInString: msg
                                                      options: 0
                                                        range: NSMakeRange(0, [msg length])
                                                 withTemplate: @"$2"];

Can anyone help me with the @"????" pattern?

1
  • What is the template format? In @[123:Dr Zoidberg], what can replace the 123 number? Commented Aug 27, 2012 at 13:50

3 Answers 3

2

This was the pattern I was after: @(.*?):(.*?)]. Thanks go to this question.

Sign up to request clarification or add additional context in comments.

Comments

0

Search for @\[123:(.*?)\] and replace with \1

Comments

0

Try replacing

@\[\d+:(.+?)\]

with the \1 group

Explanation:

Match @ followed by [, then any number of digits followed by a comma. From this point get the text until you find the closing square bracket.

If there can be any kind of whitespace between the digits and the : you can use this one

@\[\d+\t*:(.+?)\]

4 Comments

It works on notepad++. What does it match / doesn't it match? It looks a lot like the one mmdemirbas commented, maybe the problem is the square bracket at the beginning
Uhu, can there be a space (or any indentation character) between 123 and :? if so you can write it in this fashion @\[\d+\t+?:(.+?)\]
the ? means "don't be greedy with the +", in this case in fact you can omit it (my mistake, I'll edit the answer)
Think I understood: you must replace withTemplate: @"$2" with withTemplate: @"$1" telling to use the first replacing group, not the second

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.