如何从 JIRA 问题描述字段中查找表

How to find tables from JIRA issue description field

我有一个关于如何在 Jira 问题描述字段中找到 table 的开始和结束的问题。

在我的例子中,我在一张票中有三个可能的 table。

|| Responsible | abc|
|| Departement | def |

|| SystemA Username | ghi |
|| Operation | READ |
|| Page | [Example|http://www.example.com] |

|| SystemB Username | jhk |
|| Operation | WRITE |
|| App | helloWorld |

现在我没有找到如何在描述中找到所有 table 以及如何找出 table 是哪种类型(负责、A 或 B)的解决方案

From the JIRA API 将 return 一个字符串 (Java)。我认为正则表达式应该是首选。

在我的第一个版本中,我只知道如何找到 table 的开头和结尾

^\|\| \|$

是否可以分别找到所有三个 table?我可以将内容放入 key/value 集合吗?

要在单独的比赛中获得全部 3 个 table,您可以使用:

^\|\|.*\|(?:\R\|\|.*\|)*$

Regex demo

您可以获得所有 3 个 table 具有键值对的示例,例如首先拆分所有空行。

然后你可以为每个 table 使用带有 \G 锚点的模式来获取 2 个捕获组中的值,你可以将其添加到 key/value 集合中。

\G\|\|\h*(.*?)\h*\|\h*(\[[^\]\[]*\]|.*?)\h*\|\R?
  • \G 断言上一场比赛结束时的位置
  • \|\|\h*匹配||和可选空格
  • (.*?) 捕获 组 1,匹配尽可能少的没有换行符的字符
  • \h*\|\h* 匹配 | 被可选空格包围
  • ( 捕获 第 2 组
    • \[[^\]\[]*\] 从左到右方括号匹配
    • |
    • .*? 匹配尽可能少的没有换行符的字符
  • ) 关闭组 2
  • \h*\|匹配可选空格和|
  • \R? 匹配一个可选的换行符

看到一个regex demo and a Java demo

例如

HashMap<String,String> m = new HashMap<String,String>();
String regex = "\G\|\|\h*(.*?)\h*\|\h*(\[[^\]\[]*\]|.*?)\h*\|\R?";
String string = "|| Responsible | abc|\n"
        + "|| Departement | def |\n\n"
        + "|| SystemA Username | ghi |\n"
        + "|| Operation | READ |\n"
        + "|| Page | [Example|http://www.example.com] |\n\n"
        + "|| SystemB Username | jhk |\n"
        + "|| Operation | WRITE |\n"
        + "|| App | helloWorld |";

Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);       

for (String s : string.split("(?m)^\s*$")) {
    Matcher matcher = pattern.matcher(s.trim());
    while (matcher.find()) {
        m.put(matcher.group(1), matcher.group(2));
    }
}

for(Map.Entry<String, String> entry : m.entrySet()){
    System.out.println(entry.getKey() + " --> " + entry.getValue());
}

输出

App --> helloWorld
SystemB Username --> jhk
Departement --> def
SystemA Username --> ghi
Page --> [Example|http://www.example.com]
Operation --> WRITE
Responsible --> abc