質問

I'm trying to replace a character NOT AT THE START OF THE STRING, with itself followed by another character, using regexKitLite.

thisPlate = [sBasePlate 
             stringByReplacingOccurrencesOfRegex:[NSString stringWithFormat:@"([^\\^]%@)", thisChar] 
             withString:[NSString stringWithFormat:@"\1%@", thisRep]
             ];

If sBasePlate is "temp", then thisPlate gets set to emp, but I'm expecting it to be teemp

So I'm trying to replace NOT THE START OF THE STRING, followed by thisChar, with that which has been matched followed by thisRep.

Have I got my backreferences wrong? Because that's what seems to be missing. It's adding in thisRep, but ignoring the initial match and not putting it back in with \1

Sorry if I've done something really stupid and obvious, this is my first app.

役に立ちましたか?

解決

Right, I solved it. I hate answering my own questions but someone else might make the same mistake as me, so the big stupid obvious thing I missed...

back references should be $ signs.

withString:[NSString stringWithFormat:@"$1%@", prevRep]

That's how that string should be written.

Annoyingly the documentation here says that a back reference should be "\n":

http://regexkit.sourceforge.net/RegexKitLite/

But an example elsewhere shows $n. I should have guessed that. Oh well.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top