RegexKitLite 字符串开头问题

发布于 2024-11-29 03:05:16 字数 663 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

反差帅 2024-12-06 03:05:16

对了,我解决了。我讨厌回答自己的问题,但其他人可能会犯和我一样的错误,所以我错过了一个愚蠢的明显的事情......

反向引用应该是 $ 符号。

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

这就是该字符串应该写的方式。

令人烦恼的是,这里的文档说反向引用应该是“\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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文