在计算中使用正则表达式反向引用(Java、Netbeans 以及不仅如此)

发布于 2024-08-30 05:12:27 字数 356 浏览 1 评论 0原文

在 Netbeans 中,“替换命令”(ctrl+H) 有一个正则表达式复选框,以便我可以使用正则表达式在文档中搜索和替换。 我也可以使用反向引用来替换。

问题是我可以在计算中使用反向引用然后使用它吗?

例如,在“查找内容”中使用“([0-9]{1})”我将找到所有数字 在“替换为”中使用“$1a”会导致用数字本身后跟字母 a 替换所有数字。

123456--->1a2a3a4a5a6a

我想实现用 234567 替换 123456 所以我想知道我是否可以在“替换为”中使用类似 ($1 +1) 的东西,或者是否有其他方法可以做到这一点,但只能使用正则表达式(不在 MS Excel 或类似的东西中)

谢谢

In Netbeans the "Replace command"(ctrl+H) has a regular expression checkbox so that i can search and replace in the document using regex's.
Also i can replace using a backreference.

The question is can i use a backreference in a calculation and then use it?

For example with the use of "([0-9]{1})" in the "Find what" i will find all the numbers
and with the use of "$1a" in the "replace with" results in replacing all numbers with the number itself followed by the letter a.

123456--->1a2a3a4a5a6a

I want to achive replacing 123456 with 234567 so i want to know if i can use something like ($1 +1) in the "replace with" or if there is another way to do this BUT ONLY with regex(not in MS Excel or anything similar)

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

天涯沦落人 2024-09-06 05:12:27

简单的回答:不,那是不可能的。

Simple answer: no, that's not possible.

骷髅 2024-09-06 05:12:27

Notepad++ 正是这样做的。我还没有了解netbeans是如何实现这一点的。

在 Notepad++ 中,您可以在当前表达式中再次引用包含在 () 中的表达式,例如 [0-9]{1},或者使用 \1 作为第一个 () 集的替换, \2 表示第二个,依此类推。


示例:查找十六进制格式 0xAA33FF 的 RGB 值并将其转换为“#AA33FF”格式可以使用搜索表达式:0x([0-9a-fA-F]{6})和替换表达式:'#\1'

Notepad++ does this exact thing. I haven't learned how netbeans achieves this.

In Notepad++, you can reference an expression wrapped in () such as your [0-9]{1} in either the current expression again or as a replacement by using \1 for the first () set, \2 for the second and so on.


Example: looking for an RGB value in hex format 0xAA33FF and converting it to the format '#AA33FF' could be done with search expression: 0x([0-9a-fA-F]{6}) and the replace expression: '#\1'

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