正则表达式以匹配模式和文本,直到再次发生该模式,然后重复
我正在尝试为我的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
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
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.