You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解析包含换行单元格的Jira标记语言表格?

Jira表格解析问题:单元格含换行时失效

我需要解析Jira标记语言编写的表格,目前能正常解析表头和包含文本、图片(或两者组合)的单元格组成的简单表格,但遇到单元格内有换行的情况时完全失效。

现有实现代码

String JIRA_TABLE_REGEX = "(\\|\\|.*\\|\\|(\\n|\\r\\n|\\r))*(\\|.*\\|(\\n|\\r\\n|\\r)?)+";
Pattern TABLE_PATTERN = Pattern.compile(JIRA_TABLE_REGEX);
Matcher matcher = TABLE_PATTERN.matcher(input);

// TODO - fix rows with new lines
while (matcher.find()) {
    String jiraTable = matcher.group();
    // Split the input string into rows
    String[] rowArray = jiraTable.split("(\\n|\\r\\n|\\r)");
    int size = 0;
    List<String> headers = new ArrayList<>();
    List<String> cells = new ArrayList<>();

    for (String row : rowArray) {
        // If the row starts with "||" it's a header row
        if (row.startsWith("||")) {
            headers.addAll(List.of(row.substring(2, row.length() - 2).split("\\|\\|")));
            size = headers.size();
        } else if (row.startsWith("|")) {
            cells.addAll(List.of(row.substring(1, row.length() - 1).split("\\|")));
            if (size == 0) {
                size = cells.size();
            }
        }
    }
}

正则表达式说明

  • \\|\\|.*\\|\\|:匹配表格表头行
  • (\\n|\\r\\n|\\r):匹配换行符
  • )*:允许零或多个表头行
  • \\|.*\\|:匹配表格内容行
  • ?:允许零或一个换行符
  • )+:要求至少一个内容行

可正常解析的表格示例

||Heading 1 Table 1||Heading 2 Table 1||
|_*BOLD AND ITALIC*_|_Italic_|
|Normal key|Normal Value|
|Third row, *just half BOLD*| |

这段表格能正确提取每个单元格内容。

解析失效的表格示例

||Heading 1 Table 2||Heading 2 Table 2||
|Col A1|Col A2|
|SECOND ROW|Second row too|
|Row with image|!image-2023-04-24-17-51-07-167.png|width=359,height=253!|
|Row with image and text |smol  !image-2023-05-02-12-42-16-942.png|width=347,height=231! kitten|
|Row with text and new lines|Text
  
 New Line
  
 More text
 Much more text|

解析这段表格时,最后一个包含换行的单元格内容会丢失。

更新1:尝试的改进方案

修改了正则表达式和行分割规则,但仍无法提取包含换行的最后一个单元格:

private static final String JIRA_TABLE_REGEX_ALTERNATIVE = "(\\|\\|.*\\|\\|(\\n))*(\\|.*\\|(\\n)?)+";

...

rowArray = jiraTable.split("\\|\\n");

内容的提问来源于stack exchange,提问作者Ivan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 10:17:37