【问题标题】:regex to get all between @ and @ in multilines text正则表达式在多行文本中获取 @ 和 @ 之间的所有内容
【发布时间】:2021-12-10 09:11:53
【问题描述】:

希望你没事。

例如我有一个带有标签的文本 @标签 我想将标签之间的所有文本作为部分(不是一个,看最后的图片,在顶部,结果是 4 个匹配而不是一个)。问题在于每个标签的文本中可能包含符号@,这将减少我的结果匹配。

我尝试了几次不同的正则表达式,但总是没有成功

我使用的最后一个正则表达式是:

((^(@))|[^\"]@)[^(@)]+\}\n

示例centent 文本(我用于尝试):

@main.xml
{"adSize":"","adUnitId":"","alpha":1.0,"checked":0,"choiceMode":0,"clickable":1,"convert":"","customView":"","dividerHeight":1,"enabled":1,"firstDayOfWeek":1,"id":"_fab","image":{"rotate":0,"scaleType":"CENTER"},"indeterminate":"false","index":0,"inject":"","layout":{"backgroundColor":16777215,"borderColor":-16740915,"gravity"}
{"adSize":"","adUnitId":"","alpha":1.0,"checked":0,"choiceMode":0,"clickable":1,"convert":"","customView":"","dividerHeight":1,"enabled":1,"firstDayOfWeek":1,"id":"_fab","image":{"rotate":0,"scaleType":"CENTER"},"indeterminate":"false","index":0,"inject":"","layout":{"backgroundColor":16777215,"borderColor":-16740915,"gravity"}


@main.xml_fab
{"adSize":"","adUnitId":"","alpha":1.0,"checked":0,"choiceMode":0,"clickable":1,"convert":"","customView":"","dividerHeight":1,"enabled":1,"firstDayOfWeek":1,"id":"_fab","image":{"rotate":0,"scaleType":"CENTER"},"indeterminate":"false","index":0,"inject":"","layout":{"backgroundColor":16777215,"borderColor":-16740915,"gravity"}
{"adSize":"","adUnitId":"","alpha":1.0,"checked":0,"choiceMode":0,"clickable":1,"convert":"","customView":"","dividerHeight":1,"enabled":1,"firstDayOfWeek":1,"id":"_fab","image":{"rotate":0,"scaleType":"CENTER"},"indeterminate":"false","index":0,"inject":"","layout":{"backgroundColor":16777215,"borderColor":-16740915,"gravity"}
{"adSize":"","adUnitId":"","alpha":1.0,"checked":0,"choiceMode":0,"clickable":1,"convert":"","customView":"","dividerHeight":1,"enabled":1,"@firstDayOfWeek":1,"id":"_fab","image":{"rotate":0,"scaleType":"CENTER"},"indeterminate":"false","index":0,"inject":"","layout":{"backgroundColor":16777215,"borderColor":-16740915,"gravity"}


@main.xml
{"adSize":"","adUnitId":"","alpha":1.0,"checked":0,"choiceMode":0,"clickable":1,"convert":"","customView":"","dividerHeight":1,"enabled":1,"firstDayOfWeek":1,"id":"_fab","image":{"rotate":0,"scaleType":"CENTER"},"indeterminate":"false","index":0,"inject":"","layout":{"backgroundColor":16777215,"borderColor":-16740915,"gravity"}
{"adSize":"","adUnitId":"","alpha":1.0,"checked":0,"choiceMode":0,"clickable":1,"convert":"","customView":"","dividerHeight":1,"enabled":1,"firstDayOfWeek":1,"id":"_fab","image":{"rotate":0,"scaleType":"CENTER"},"indeterminate":"false","index":0,"inject":"","layout":{"backgroundColor":16777215,"borderColor":-16740915,"gravity"}

j
@main.xml_fab
{"adSize":"","adUnitId":"","alpha":1.0,"checked":0,"choiceMode":0,"clickable":1,"convert":"","customView":"","dividerHeight":1,"enabled":1,"firstDayOfWeek":1,"id":"_fab","image":{"rotate":0,"scaleType":"CENTER"},"indeterminate":"false","index":0,"inject":"","layout":{"backgroundColor":16777215,"borderColor":-16740915,"gravity"}
{"adSize":"","adUnitId":"","alpha":1.0,"checked":0,"choiceMode":0,"clickable":1,"convert":"","customView":"","dividerHeight":1,"enabled":1,"firstDayOfWeek":1,"id":"_fab","image":{"rotate":0,"scaleType":"center"},"indeterminate":"false","index":0,"inject":"","layout":{"backgroundColor":16777215,"borderColor":-16740915,"gravity"}
{"adSize":"","adUnitId":"","alpha":1.0,"checked":0,"choiceMode":0,"clickable":1,"convert":"","customView":"","dividerHeight":1,"enabled":1,"firstDayOfWeek":1,"id":"_fab","image":{"rotate":0,"scaleType":"CENTER"},"indeterminate":"false","index":0,"inject":"","layout":{"backgroundColor":16777215,"borderColor":-16740915,"gravity"}

这是图片显示我的问题

我想得到@tag1 和@tag2,3,4...等之间的全部部分, 如果符号@在标签的内容内,我该如何处理?

【问题讨论】:

    标签: java android regex


    【解决方案1】:

    我不太确定您在表达中使用了[^\"]@...它的目的是什么?你能举个例子吗?

    无论如何,您必须从排除字符列表中删除 @。并改变你在部分结束时的样子。试试:

    ^@[^(]+?\}\n(?=\@|$)
    
    • ^@ 匹配行首的@
    • [^(]+? 匹配除 ( 之外的任何字符一次或多次,直到(? 使其变得懒惰)下一个元素的下一次出现...
    • \}\n 您的段总是以大括号结尾,后跟新行
    • (?=\@|$) 关键部分:确保以下元素是 @(新段)或文件末尾的前瞻,而不捕获它。这样一来,您就不会在一段结束之前剪切一段,您仍然可以捕捉到下一段的开头。

    试试Demo

    【讨论】:

      【解决方案2】:

      使用

      (?m)^@.+(?:\n.+)*
      

      regex proof

      解释

      --------------------------------------------------------------------------------
        ^                        the beginning of the line due to (?m)
      --------------------------------------------------------------------------------
        @                        '@'
      --------------------------------------------------------------------------------
        .+                       any character except \n (1 or more times
                                 (matching the most amount possible))
      --------------------------------------------------------------------------------
        (?:                      group, but do not capture (0 or more times
                                 (matching the most amount possible)):
      --------------------------------------------------------------------------------
          \n                       '\n' (newline)
      --------------------------------------------------------------------------------
          .+                       any character except \n (1 or more times
                                   (matching the most amount possible))
      --------------------------------------------------------------------------------
        )*                       end of grouping
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多