Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何防止正则表达式转换bbcode超链接?_Java_Regex - Fatal编程技术网

Java 如何防止正则表达式转换bbcode超链接?

Java 如何防止正则表达式转换bbcode超链接?,java,regex,Java,Regex,我有一个正则表达式,它将解析消息的内容,并将纯文本超链接转换为HTML超链接 message = message.replaceAll("(?:https?|http?)://[\\w/%.\\-?&=!#]+", "<a href='$0' target='_blank'>$0</a>"); message=message.replaceAll(“(?:https?| http?)://[\\w/%.\

我有一个正则表达式,它将解析消息的内容,并将纯文本超链接转换为HTML超链接

message = message.replaceAll("(?:https?|http?)://[\\w/%.\\-?&=!#]+",
                             "<a href='$0' target='_blank'>$0</a>");
message=message.replaceAll(“(?:https?| http?)://[\\w/%.\\-?&=!\\]+”,
"");
转换http或https超链接时,这是正常的

message = message.replaceAll("(?:https?|http?)://[\\w/%.\\-?&=!#]+",
                             "<a href='$0' target='_blank'>$0</a>");
问题是,我也有其他超链接包含在bbcode标记中,我不希望正则表达式转换为HTML超链接

message = message.replaceAll("(?:https?|http?)://[\\w/%.\\-?&=!#]+",
                             "<a href='$0' target='_blank'>$0</a>");
例如,
[IMG]http://www.google.com/img.png[/IMG]
[YOUTUBE]http://www.youtube.com[/YOUTUBE]


如何更改正则表达式以防止正则表达式转换bbcode超链接?

您可以使用反向查找

"(?<!\])(?:https?|http?)://[\\w/%.\\-?&=!#]+"

”(?我建议像这样使用负前瞻,以避免匹配bbcode链接:

message = message.replaceAll("(?:https?|http?)://[\\w/%.\\-?&=!#]+(?!.*\\[/)",
                         "<a href='$0' target='_blank'>$0</a>");
message=message.replaceAll((?:https?;http?://[\\w/%.\\-?&=!\\]+(?!!.\\[/)”,
"");