---
title: "java版的正规UBB"
author: "ater"
date: "2007-03-08T15:45:47Z"
tags: []
slug: "java-ubb"
draft: false
lastmod: "2007-03-08T15:45:47Z"
id: 58251
url: "https://zsy.iteye.com/blog/58251"
---

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.util.regex.Matcher;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.util.regex.Pattern;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) * @author zsy  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif) \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public final class Ubbcode ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private static final String imagePath = "images/"; // 定义图片路径  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private static final String strIcons = "1";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private static final String picUrl = "edit/images/";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private static final String imgName = "edit/images/face/";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private static final String notePath = "http://www.richmap.com.cn/richmap4/api/tools/";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private static String ubbItem(String strContent, String re, String replayStr,   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            Pattern pattern, Matcher matcher, boolean IgnoreCase) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        pattern = Pattern.compile(re);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        matcher = pattern.matcher(strContent);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        strContent = matcher.replaceAll(replayStr);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        if (IgnoreCase) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            pattern = Pattern.compile(re.toUpperCase());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            matcher = pattern.matcher(strContent);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            strContent = matcher.replaceAll(replayStr);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return strContent;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private static String quote(String strContent, Pattern pattern,   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            Matcher matcher, boolean IgnoreCase) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        String re = "(\\\[quote\\])(.\*?)(\\\[\\/quote\\])";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        int start = strContent.lastIndexOf("\[quote]");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        int end = strContent.indexOf("\[/quote]");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        String subStr = null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        String newStr = null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        while (start != -1 && end != -1) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            subStr = strContent.substring(start, end + 8);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            pattern = Pattern.compile(re);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            matcher = pattern.matcher(subStr);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            newStr = matcher.replaceAll("&lt;TABLE style="BORDER-RIGHT: #cccccc 1px dotted; " +  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    "TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: " +  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    "#cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellSpacing=0 " +  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    "cellPadding=6 width=95% align=center border=0&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD " +  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    "style="WORD-WRAP: break-word;font-size:12px;" bgColor=#f3f3f3&gt;&lt;FONT " +  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    "style="FONT-WEIGHT: bold; COLOR: #990000; font-size:12px;"&gt;\*\*\*以下是引" +  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    "用\*\*\*&lt;/FONT&gt;&lt;HR width=95% style="border:1px dotted  #cccccc "&gt;&lt;center&gt;" +  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    "$2&lt;/center&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            strContent = strContent.replace(subStr, newStr);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            start = strContent.lastIndexOf("\[quote]");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            end = strContent.indexOf("\[/quote]");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        if (IgnoreCase) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            re = "(\\\[QUOTE\\])(.\*?)(\\\[\\/QUOTE\\])";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            start = strContent.lastIndexOf("\[QUOTE]");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            end = strContent.indexOf("\[/QUOTE]");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            while (start != -1 && end != -1) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                subStr = strContent.substring(start, end + 8);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                pattern = Pattern.compile(re);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                matcher = pattern.matcher(subStr);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                newStr = matcher.replaceAll("&lt;TABLE style="BORDER-RIGHT: #cccccc 1px dotted; " +  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                        "TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: " +  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                        "#cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellSpacing=0 " +  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                        "cellPadding=6 width=95% align=center border=0&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD " +  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                        "style="WORD-WRAP: break-word;font-size:12px;" bgColor=#f3f3f3&gt;&lt;FONT " +  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                        "style="FONT-WEIGHT: bold; COLOR: #990000; font-size:12px;"&gt;\*\*\*以下是引" +  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                        "用\*\*\*&lt;/FONT&gt;&lt;HR width=95% style="border:1px dotted  #cccccc "&gt;&lt;center&gt;" +  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                        "$2&lt;/center&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                strContent = strContent.replace(subStr, newStr);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                start = strContent.lastIndexOf("\[QUOTE]");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                end = strContent.indexOf("\[/QUOTE]");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)            }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return strContent;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    /\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @description 转换代码，使之支持ubbcode.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param strContent  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     *            转换前的代码.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @return result 转换后的内容.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public static String ubbCode(String strContent) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        strContent = dvHTMLEncode(strContent);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        Pattern pattern = null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        Matcher matcher = null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        //随意贴  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        strContent = ubbItem(strContent,   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                "\\\[richmapfrm=(.\[^\\\[]\*)\\](.+?)\\\[\\/richmapfrm\\]",   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                "&lt;iframe src=" + notePath + "singleNote.jsp?w=600&h=480" +  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                "&noteid=$1 frameborder=0 marginheight=0 marginwidth=0 scrolling=no height=400 " +  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                "width=600 title=睿图随意贴,贴出你的故事,贴出你的心情 target=\_blank&gt;&lt;img src=" +   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                picUrl + "button/richiframe.gif border=0&gt; $2&lt;/iframe&gt;",   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                pattern, matcher, true);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        strContent = ubbItem(strContent,   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                "\\\[richmap=(.\[^\\\[]\*)\\](.+?)\\\[\\/richmap\\]",   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                "&lt;a href=" + notePath + "myNote.jsp?&noteid=$1 " +  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                "title=睿图随意贴,贴出你的故事,贴出你的心情 target=\_blank&gt;&lt;img src=" + picUrl +   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                "button/richwindow.gif border=0&gt;$2&lt;/a&gt;",   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                pattern, matcher, true);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        //转换IMG  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        strContent = ubbItem(strContent,   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                "\\\[img\\](.+?)\\\[\\/img\\]",   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                "&lt;a href="$1" target="\_blank"&gt;" +  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                "&lt;IMG SRC="$1" border="0" alt="按此在新窗口浏览图片" " +  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                "onload="javascript:if(this.width&gt;600)this.width=600" " +  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                "galleryImg="no"&gt;&lt;/a&gt;",   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                pattern, matcher, true);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        //转换附件\[FILE=路径,图片]名称\[/FILE]  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        strContent = ubbItem(strContent,   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                "(\\\[file=(.\[^\\\[]\*),(.\[^\\\[]\*)\\])(.\*?)(\\\[\\/file\\])",   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                "&lt;br&gt;&lt;IMG SRC=$3 border=0&gt;&lt;br&gt; &lt;a href=$2&gt;$4&lt;/a&gt;",   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                pattern, matcher, true);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        //DIR  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        strContent = ubbItem(strContent,   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                "\\\[dir=\*(\[0-9]\*),\*(\[0-9]\*)\\](.\*?)\\\[\\/dir]",   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                "&lt;object classid=clsid:" +  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                "166B1BCA-3F9C-11CF-8075-444553540000 codebase=" +  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                "http://download.macromedia.com/pub/shockwave/cabs/director/" +  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                "sw.cab#version=7,0,2,0 width=$1 height=$2&gt;&lt;param name=src " +  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                "value=$3&gt;&lt;embed src=$3 pluginspage=http://www.macromedia.com/" +  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                "shockwave/download/ width=$1 height=$2&gt;&lt;/embed&gt;&lt;/object&gt;",   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                pattern, matcher, true);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        //QT  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        strContent = ubbItem(strContent,   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                "\\\[qt=\*(\[0-9]\*),\*(\[0-9]\*)\\](.\*?)\\\[\\/qt]",   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                "&lt;embed src=$3 width=$1 height=$2 " +  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                "autoplay=true loop=false controller=true playeveryframe=false " +  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                "cache=false scale=TOFIT bgcolor=#000000 kioskmode=false " +  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                "targetcache=false pluginspage=http://www.apple.com/quicktime/&gt;",   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                pattern, matcher, true);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        //MP  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        strContent = ubbItem(strContent,   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                "\\\[mp=\*(\[0-9]\*),\*(\[0-9]\*)\\](.\*?)\\\[\\/mp]",   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                "&lt;object align=middle classid=CLSID:" +  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)