简体   繁体   English

使用正则表达式进行Java解析Wiki语法

[英]Java Parsing Wiki Syntax with regex

I'm making an application to generate HTML document from wiki markup . 我正在制作一个从Wiki标记生成HTML文档的应用程序。 And I figured regex was the right way to replace markup to HTML tags. 而且我认为正则表达式是将标记替换为HTML标签的正确方法。 I came up with a working regex to find markup, but I couldn't find Java API to replace it while retaining the sentence. 我想出了一个可以使用的正则表达式来查找标记,但是在保留句子的同时我找不到Java API来替换它。

The regex suggested by an answer: 正则表达式由答案提示:
--([^-]+)-- (regex --([^-])+-- doesn't behave well with Java API. Moving + inside the capturing group solves the problem.) --([^-]+)-- (正则表达式--([^-])+--在Java API中表现不佳。在捕获组中移动+可解决此问题。)

Example: 例:
--This is strike-- should be <strike>This is strike</strike> --This is strike-- <strike>This is strike</strike>应该是<strike>This is strike</strike>
But cases like This is ---- normal text or ---Triple hyphens--- should NOT be replaced. 但是,不应替换This is ---- normal text ---Triple hyphens--- This is ---- normal text---Triple hyphens--- However, cases like --striked----also striked-- works. 但是,类似- --striked----also striked--这样的--striked----also striked--奏效。

Only Java 6 please. 请只使用Java 6。
Any help is welcome. 欢迎任何帮助。 Thanks in advance. 提前致谢。

EDIT: Based on an answer, it seems I can use $1 for this purpose. 编辑:根据一个答案,看来我可以为此目的使用$1

Groups (...) are numbered from 1 in order of occurrence. (...)按出现的顺序从1开始编号。 Postfix operator * = 0 or more, + is 1 or more. 后缀运算符* = 0或更大, +是1或更大。

s = s.replaceAll("--([^-]+)--", "<strike>$1</strike>");

After asking one of my friend, I got an answer. 问了我的一个朋友后,我得到了答案。

--([^-]+(-[^-]+)*)-- Will work correctly and capture subject string as $1 --([^-]+(-[^-]+)*)--将正常工作,并将主题字符串捕获为$1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM