RegexKitLite 字符串开头问题
我正在尝试使用 regexKitLite 替换不在字符串开头的字符,其自身后跟另一个字符。
thisPlate = [sBasePlate
stringByReplacingOccurrencesOfRegex:[NSString stringWithFormat:@"([^\\^]%@)", thisChar]
withString:[NSString stringWithFormat:@"\1%@", thisRep]
];
如果 sBasePlate 为“temp”,则 thisPlate 将设置为 emp,但我希望它为 teemp >
因此,我尝试将 NOT THE START OF THE STRING(后跟 thisChar)替换为已匹配的内容,后跟 thisRep。
我的反向引用有错吗?因为这似乎就是缺失的东西。它添加了 thisRep,但忽略了初始匹配,并且不将其放回 \1
抱歉,如果我做了一些非常愚蠢和明显的事情,这是我的第一个应用程序。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对了,我解决了。我讨厌回答自己的问题,但其他人可能会犯和我一样的错误,所以我错过了一个愚蠢的明显的事情......
反向引用应该是 $ 符号。
这就是该字符串应该写的方式。
令人烦恼的是,这里的文档说反向引用应该是“\n”:
http://regexkit.sourceforge.net/ RegexKitLite/
但其他地方的示例显示 $n。我早该猜到的。那好吧。
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.
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.