求援:请教正则式高手!!!

aploo 2009-03-27 11:26:47
求援:请教正则式高手!!!

有一个路径,要动态生成页号,正则式如何写:

// 源路径:
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替换[页码]为数字。但是不知正则式该如何写好?
...全文
85 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
coodd 2009-03-27
  • 打赏
  • 举报
回复
p与.html之间不就是页码?这么规律不用正则表达了
直接Url.Remove(Url.LastIndexOf('p')+1)+"你要输入的页码"+".html"
不就行了吗
aploo 2009-03-27
  • 打赏
  • 举报
回复
谢谢了.
wackyboy 2009-03-27
  • 打赏
  • 举报
回复

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";
*/

wackyboy 2009-03-27
  • 打赏
  • 举报
回复

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";
*/

不知道理解的对不对
wackyboy 2009-03-27
  • 打赏
  • 举报
回复
我没看明白是将什么样的字符串转换成什么样的
orain 2009-03-27
  • 打赏
  • 举报
回复
(?<!\d)\d+(?=\.html$) 匹配到的就是页码了。
aploo 2009-03-27
  • 打赏
  • 举报
回复
谢谢各位啊... 但是都没能解决问题. 郁闷...

网上的许多例子似呼和我的要求都不同.... 真是难啊....
hoojo 2009-03-27
  • 打赏
  • 举报
回复

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));
}
}
这个比较常用的
兄弟 你看看 对你有没有帮助
^_*
outou 2009-03-27
  • 打赏
  • 举报
回复
aploo 2009-03-27
  • 打赏
  • 举报
回复
例如:string input = "news/detail/v5000441-d1001358026-1.html";

例如:string input = "news/detail/v5000441-d1001358026_1.html";

页号可能会有多种的形式...这时写死的话,就比较麻烦了...
aploo 2009-03-27
  • 打赏
  • 举报
回复
我目前也是这样写的, 但这样会写死, 因为页号有没有P是不确定的,而且页号具体位置也难以确定, 因此才需要用正则式的形式啊...

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧