方法:
private String replace (String content, String pattern, String replace)
{
try
{
pattern1 = patterCom.compile(pattern, Perl5Compiler.CASE_INSENSITIVE_MASK); //后面参数,创建大小写不敏感的模式
content = Util.substitute(matcher,
pattern1,
new Perl5Substitution(replace, Perl5Substitution.INTERPOLATE_ALL),
content, Util.SUBSTITUTE_ALL);
}
catch (Exception e)
{
e.printStackTrace();
}
return content;
}调用测试:String str = replace(inputStr, "\\[IMG\\](.+)\\[/IMG\\]", "<img src=\"$1\">");如果 inputStr = "";
则正常
如果 inputStr = "";
就成了 <img src="imagesrc.jpg[/IMG][IMG]imagesrc.jpg");
即,拿第一段[IMG]与最后一段[/IMG] 匹配去了
问一下,怎样改决
private String replace (String content, String pattern, String replace)
{
try
{
pattern1 = patterCom.compile(pattern, Perl5Compiler.CASE_INSENSITIVE_MASK); //后面参数,创建大小写不敏感的模式
content = Util.substitute(matcher,
pattern1,
new Perl5Substitution(replace, Perl5Substitution.INTERPOLATE_ALL),
content, Util.SUBSTITUTE_ALL);
}
catch (Exception e)
{
e.printStackTrace();
}
return content;
}调用测试:String str = replace(inputStr, "\\[IMG\\](.+)\\[/IMG\\]", "<img src=\"$1\">");如果 inputStr = "";
则正常
如果 inputStr = "";
就成了 <img src="imagesrc.jpg[/IMG][IMG]imagesrc.jpg");
即,拿第一段[IMG]与最后一段[/IMG] 匹配去了
问一下,怎样改决
不知道你这程序段的上下都是什么,所以给你一段程序参考一下:
对了,得是SDK 1.5 下调试
import java.util.*;
public class Example
{
public static void main(String args[ ])
{
String s="we are stud,ents";
StringTokenizer fenxi=new StringTokenizer(s," ,"); //空格和逗号做分
int number=fenxi.countTokens();
while(fenxi.hasMoreTokens())
{
String str=fenxi.nextToken();
System.out.println(str);
System.out.println("还剩"+fenxi.countTokens()+"个单词");
}
System.out.println("s共有单词:"+number+"个");
}
}
我的信箱:[email protected]
后来,又觉得太烦琐了,就改为用正则表达式
应该是这个
import java.util.regex.*;
class Example
{
public static void main(String args[ ])
{
Pattern p; //模式对象。
Matcher m; //匹配对象。
String s1="0A1A2A3A4A5A6A7A8A9"; //待匹配的字符序列。
p=Pattern.compile("\\dA\\d"); //用模式"\\dA\\d"初试化模式对象。
m=p.matcher(s1); //用待匹配字符序列初始化匹配对象。
while(m.find())
{
String str=m.group();
System.out.print("从"+m.start()+"到"+m.end()+"匹配模式子序列:");
System.out.println(str);
}
String temp=m.replaceAll("***");
System.out.println(temp);
System.out.println(s1);
m=p.matcher("9A00A3"); //重新初始化匹配对象。
if(m.matches())
{
String str=m.group();
System.out.println(str);
}
else
{
System.out.println("不完全匹配");
}
if(m.lookingAt())
{
String str=m.group();
System.out.println(str);
}
}
}
输出<<== <a href="http://url"/> 输入: http://111wwwhttp://222http://333
输出: <a href="http://111"/>www<a href="http://222"/><a href="http://333"/>这就是我要的结果可用正则一配,就成了:<a href="http://111[/url]wwwhttp://222[url]http://333"/>我现在想到一个办
也就是在写正则表的写法 在 \\[url\\](.+)\\[/url\\] 中间,不能含有\\[url\\](.+)\\[/url\\]才匹配,可是这个正则,不晓得是何时写
正则表达式: \[IMG\]([^\[IMG\]|\[/IMG\]]+)\[/IMG\]
运行结果是
从0到3匹配模式子序列: 0A1
从4到7匹配模式子序列: 2A3
从8到11匹配模式子序列: 4A5
从12到15匹配模式子序列: 6A7
从16到19匹配模式子序列: 8A9
***A***A***A***A***
0A1A2A3A4A5A6A7A8A9
不完全匹配
9A0
你用这个配出来的结果是什么啊?
对我的用处不大,我的标签是成对的,不过,还是感谢你\[IMG\]([^\[IMG\]]+)\[/IMG\]
就是匹配[IMG]和[/IMG]中间不含 [IMG]的段唉
我又弄错了,认为 [^\[IMG\]]是说不能出现 "[IMG]",可实际上,是不能出现 '[' or 'I' or,'M',or 'G' or ']'
而非限制"[IMG]"的连续出现
现在想法办法,限[IMG]不能连续出现,问题就真的解决了
这个正则真TMD的难写,
Matcher match;
pattern=Pattern.compile("<a HREF=http://.*>*.待检索字段.*</a>",Pattern.CASE_INSENSITIVE);
match=pattern.matcher(str);
while(match.find())
{}怎么说呢,我是想让你注意这个方法。match.find() 是说“如果下面有就继续找出来”
这点时间我也学到了很多东西
你看看这么配行不行?
pattern=Pattern.compile("
"[IMG]imagesrc.jpg" alt="" />";
Matcher match;
pattern = Pattern.compile("\\[IMG\\].*?\\[/IMG\\]");
match = pattern.matcher("");
while (match.find()) {
System.out.println(match.group());
}