求援:请教正则式高手!!!有一个路径,要动态生成页号,正则式如何写:// 源路径:
string input = "news/detail/v5000441-d1001358026-p1.html";// 表达式:
string pattern = "news/detail/[变量]-p[页码].html";// 由于:v5000441-d1001358026是动态的,因此用@"[\s\S]*?"代替
pattern = Regex.Escape(pattern).Replace("[变量]", @"[\s\S]*?");// 构成正则式
Regex r = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);现在我想要的结果是:"news/detail/v5000441-d1001358026-p[页码].html"这样好用Replace替换[页码]为数字。但是不知正则式该如何写好?
string input = "news/detail/v5000441-d1001358026-p1.html";// 表达式:
string pattern = "news/detail/[变量]-p[页码].html";// 由于:v5000441-d1001358026是动态的,因此用@"[\s\S]*?"代替
pattern = Regex.Escape(pattern).Replace("[变量]", @"[\s\S]*?");// 构成正则式
Regex r = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);现在我想要的结果是:"news/detail/v5000441-d1001358026-p[页码].html"这样好用Replace替换[页码]为数字。但是不知正则式该如何写好?
直接Url.Remove(Url.LastIndexOf('p')+1)+"你要输入的页码"+".html"
不就行了吗
或
例如:string input = "news/detail/v5000441-d1001358026_1.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));
}
}
这个比较常用的
兄弟 你看看 对你有没有帮助
^_*
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";
*/