正则表达式以匹配模式和文本,直到再次发生该模式,然后重复

发布于 2025-01-28 23:35:56 字数 405 浏览 1 评论 0原文

我正在尝试为我的Kotlin/JVM程序编写一条正则表达式:
给定文本行{#ff00ff} test1 {#112233} {占位符} test2
它应该匹配:
匹配1:<代码>#ff00ff 作为第1组和test1作为第2组
匹配2:#112233作为组1和{占位符} test2作为组2

#ff00ff可以是任何有效的6个字符HEX颜色代码。

我正在努力的事情是在颜色图案之后匹配文本,直到出现另一种颜色图案为止。
当前的正则我想出的是\ {(#[#[A-ZA-Z0-9] {6})\}((?!\ {#[A-ZA-Z0-9] )。*)

I'm trying to write a regex for my Kotlin/JVM program that satisfies:
Given this line of text {#FF00FF}test1{#112233}{placeholder} test2
It should match:
Match 1: #FF00FF as group 1 and test1 as group 2
Match 2: #112233 as group 1 and {placeholder} test2 as group 2

#FF00FF can be any valid 6 character hex color code.

The thing I'm struggling with is to match the text after the color pattern until another color pattern comes up.
Current regex I came up with is \{(#[a-zA-Z0-9]{6})\}((?!\{#[a-zA-Z0-9]{6}\}).*)

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

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

发布评论

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

评论(1

骄傲 2025-02-04 23:35:56

您可以使用

\{(#[a-zA-Z0-9]{6})\}(.*?)(?=\{#[a-zA-Z0-9]{6}\}|$)

regex demo 详细信息

  • \ { - a { char
  • (#[A-ZA-Z0-9] {6}) - 组1:a char和六个alphanumerics
  • \} - a } char char
  • (。*?) - 第2组:除换期折扣以外的任何零或更多的字符
  • (?= \ {#[a-za-z0-9] {6} \} \} | $) -位置立即遵循{,六个字母数字和} char或字符串的结尾。

You can use

\{(#[a-zA-Z0-9]{6})\}(.*?)(?=\{#[a-zA-Z0-9]{6}\}|$)

See the regex demo. Details:

  • \{ - a { char
  • (#[a-zA-Z0-9]{6}) - Group 1: a # char and six alphanumerics
  • \} - a } char
  • (.*?) - Group 2: any zero or more chars other than line break chars as few as possible
  • (?=\{#[a-zA-Z0-9]{6}\}|$) - a position immediately followed with a {, #, six alphanumerics and a } char, or end of string.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文