111,126
社区成员
发帖
与我相关
我的任务
分享
string str = "例如:string input = \"news/detail/v5000441-d1001358026-1.html\"; 或 例如:string input = \"news/detail/v5000441-d1001358026_15.html\";"
Regex re = new Regex(@"\S(\d+)(?=\.html)");
string output = re.Replace(str,"-p[$1]");
/* output为
例如:string input = "news/detail/v5000441-d1001358026-p[1].html"; 或 例如:string input = "news/detail/v5000441-d1001358026-p[15].html";
*/
string str = @"
例如:string input = \"news/detail/v5000441-d1001358026-1.html\";
或
例如:string input = \"news/detail/v5000441-d1001358026_15.html\";"
Regex re = new Regex(@"\S(\d+)(?=\.html)");
string output = re.Replace(str,"-p[$1]");
/* output为
例如:string input = "news/detail/v5000441-d1001358026-p[1].html";
或
例如:string input = "news/detail/v5000441-d1001358026-p[15].html";
*/
package com.hoo.util;
import java.util.HashMap;
import org.apache.oro.text.regex.*;
public class SetImg {
/**
* 替换字符
* @param text 要被替换的字符
* @param patternString 正则条件
* @param map 替换的字符集合
* @return 替换后的字符
*/
public String convert(String text, String patternString, HashMap map){
PatternMatcher matcher;
PatternCompiler compiler;
Pattern pattern;
PatternMatcherInput input;
MatchResult result;
StringBuffer textBuf = new StringBuffer(text);
try{
compiler = new Perl5Compiler();
pattern = compiler.compile(patternString);
matcher = new Perl5Matcher();
input = new PatternMatcherInput(text.toString());
int offset = 0;
while (matcher.contains(input, pattern)) {
result = matcher.getMatch();
int len = result.length();
String key = result.toString();
String value = (String) map.get(key);
textBuf.replace(result.beginOffset(0) + offset, result.endOffset(0)
+ offset, value);
offset += value.length() - len;
}
}catch (Exception e) {
e.printStackTrace();
}
return textBuf.toString();
}
//设置表情
public static String getContent(String content){
SetImg si = new SetImg();
String pattern = "\\{[a-zA-Z0-9]+\\}";
HashMap map = new HashMap();
for (int i = 1;i <= 24;i++){
if (i < 10){
map.put("{face20" + i +"}", "<img src='img/face20" + i + ".gif'/>");
}
if (i >= 10 && i <= 24){
map.put("{face2" + i +"}", "<img src='img/face2" + i + ".gif'/>");
}
}
return si.convert(content, pattern, map);
}
public static void main(String[] args) throws MalformedPatternException {
HashMap map = new HashMap();
/*for (int i = 1;i <= 23;i++){
if (i < 10){
map.put("{face20" + i +"}", "img/face20" + i + ".jsp");
}
if (i >= 10 && i <= 99){
map.put("{face2" + i +"}", "img/face2" + i + ".jsp");
}
}
for (int i = 1;i < 24;i++){
if (i < 10){
System.out.println(map.get("{face20" + i +"}"));
}
if (i >= 10 && i < 99){
System.out.println(map.get("{face2" + i +"}"));
}
}
String pattern = "\\{[a-zA-Z0-9]+\\}";*/
String text = "ab bbaa {face201} {face203} {face206} na na #hha# bbana {face202}";
//System.out.println((new SetImg()).convert(text, pattern, map));
System.out.println(SetImg.getContent(text));
}
}
这个比较常用的
兄弟 你看看 对你有没有帮助
^_*