比如字符串是这样的:
数据/title,结构/title,人民邮电出版社/publisher,9878976688/ISBN,作者一/author,作者二/author……我现在想把同样状态的合并起来,如:
数据结构/title,人民邮电出版社/publisher,9878976688/ISBN,作者一,作者二/author……我用数组做的,但觉得不是很好的方法,大家有没有更好的方法??
数据/title,结构/title,人民邮电出版社/publisher,9878976688/ISBN,作者一/author,作者二/author……我现在想把同样状态的合并起来,如:
数据结构/title,人民邮电出版社/publisher,9878976688/ISBN,作者一,作者二/author……我用数组做的,但觉得不是很好的方法,大家有没有更好的方法??
解决方案 »
- java关于图像问题的菜鸟问题
- 写一个接口,给人家用httpclient请求,然后以xml形式返回给给人家
- java线程数据获问题
- java7要发布了,大家来说说性特性吧
- c/s下多文件多线程传输问题
- 求救高手----急---^^
- 转让培训机构内部视频培训录像(JAVA+Oracle)和相关书籍,要的进啊~~~
- 一个有关jdbc的RMI的问题,JAVA高手指点一下
- java的应用调查,每一位发言者最低得到5分,不够我会再加分的。
- 急!急!急!请大虾指点:我用JBuilder3写了个Applet,单单运行Applet能正常显示,但运行HTML就提示找不到aaa.class,我的codebase路径是通过‘搜索’查出来的,应该没错的,那到底是什么原因?
- java.lang.String 方法split使用问题
- 请问怎么做ServerSocket+Socket在一个文件里面呢??
{数据结构/title,人民邮电出版社/publisher,9878976688/ISBN,(作者一,作者二)/author……}
遇到key相同的就合并咯
比如,key都为/title 就数据+结构
import java.util.regex.Pattern;
import java.util.regex.Matcher;public class Test{
public static void main(String[] args){
String message = "数据/title,结构/title,人民邮电出版社/publisher,9878976688/ISBN,作者一/author,我添加的/myadd,作者二/author,作者三/author";
//数据结构/title,人民邮电出版社/publisher,9878976688/ISBN,(作者一,作者二)/author
String array[] = message.split(",");
int length = array.length;
String regex = "^(.*,)?(.*?)/(\\w+)(.*)?,(.*?)/(\\3)(.*)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = null;
while(length > 0){
matcher = pattern.matcher(message);
if(matcher.find()){
if(matcher.group(2).equals(matcher.group(5))){
message = matcher.group(1) + matcher.group(2) + "/" + matcher.group(3) + matcher.group(4)+matcher.group(7);
}else{
String prefix = matcher.group(2) + "|" + matcher.group(5);
prefix = prefix.replaceAll("[()]","");
message = matcher.group(1) + "(" + prefix + ")" + "/" +matcher.group(3) + matcher.group(4) + matcher.group(7);
}
}
length --;
}
//这里因为有^(.*,)?的存在,所以会出现message前面的null,要把他去掉
message = message.replaceAll("^null(.*)","$1"); System.out.println(message);
//结果为 : (数据|结构)/title,人民邮电出版社/publisher,9878976688/ISBN,(作者一|作者二|作者三)/author,我添加的/myadd
}
}可以用正则处理
import java.util.regex.Pattern;
import java.util.regex.Matcher;public class Test{
public static void main(String[] args){
String message = "数据/title,结构/title,人民邮电出版社/publisher,9878976688/ISBN,作者一/author,我添加的/myadd,作者二/author,作者三/author";
//数据结构/title,人民邮电出版社/publisher,9878976688/ISBN,(作者一,作者二)/author
String array[] = message.split(",");
int length = array.length;
String regex = "^(.*,)?(.*?)/(\\w+)(.*)?,(.*?)/(\\3)(.*)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = null;
while(length > 0){
matcher = pattern.matcher(message);
if(matcher.find()){
if(matcher.group(2).equals(matcher.group(5))){
message = matcher.group(1) + matcher.group(2) + "/" + matcher.group(3) + matcher.group(4)+matcher.group(7);
}else{
String prefix = matcher.group(2) + "|" + matcher.group(5);
prefix = prefix.replaceAll("[()]","");
message = matcher.group(1) + "(" + prefix + ")" + "/" +matcher.group(3) + matcher.group(4) + matcher.group(7);
}
}
length --;
}
//这里因为有^(.*,)?的存在,所以会出现message前面的null,要把他去掉
message = message.replaceAll("^null(.*)","$1"); System.out.println(message);
//结果为 : (数据|结构)/title,人民邮电出版社/publisher,9878976688/ISBN,(作者一|作者二|作者三)/author,我添加的/myadd
}
}可以用正则处理
比如还有其他的:price,page等状态,你的正则表达式不是还得变么?
package com.test;import java.util.Enumeration;
import java.util.regex.Matcher;
import java.util.regex.Pattern;import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;public class Other {
public static void main(String[] args) {
String str = "数据/title,结构/title,人民邮电出版社/publisher,9878976688/ISBN,作者一/author,测试1/p,作者二/author,价格1/price,价格2/price,测试/p";
printNewStr(str);
}
public static void printNewStr(String str){
StringBuffer sb = new StringBuffer();
String reg = "([^\\/,]+)\\/([^\\/,]+)";
Matcher matcher = Pattern.compile(reg).matcher(str);
Hashtable ht = new Hashtable();
while(matcher.find()){
if(ht.containsKey(matcher.group(2))){
ht.put(matcher.group(2), ht.get(matcher.group(2)) + "," + matcher.group(1));// 内容之间加上逗号
}else{
ht.put(matcher.group(2), matcher.group(1));
}
}
Enumeration keys = ht.keys();
while(keys.hasMoreElements()){
Object key = keys.nextElement();
String value = ht.get(key).toString();
sb.append((value.indexOf(",") >= 0 ? "(" + value + ")" : value) + "/" + key + ",");// 如果有多个内容,则加括号
}
System.out.println(sb.toString().replaceAll(",$", ""));
}
}
([^\\/,]+)\\/([^\\/,]+)
\\/, 表示 / 或者 ,
[^\\/,]+ 不是 / 或 , 的字符至少出现一次
([^\\/,]+) 分组,对应 group(1),后面那个对应 group(2)7楼:
^(.*,)?(.*?)/(\\w+)(.*)?,(.*?)/(\\3)(.*)
^ 字符串开始
.* 任意字符出现 0 次或多次
(.*,)? 前面表达式出现 0 次或 1 次
\\w 匹配字母、数字、下划线
\\3 没用过
这里括号括起来的,同样表示分组 group(x) x=1,2,3,4,5,6,7
再问问,如果得到了这样的结果
(数据,结构)/title,人民邮电出版社/publisher,9878976688/ISBN,(作者一,作者二,作者三)/author 等等……
如果写正则表达式提取出(作者一,作者二,作者三),(数据,结构)之类的信息?
我知道从你的hashtable里可以直接取出,但我想学习一下正则表达式
String str = "(数据,结构)/title,人民邮电出版社/publisher,9878976688/ISBN,(作者一,作者二,作者三)/author";
String reg = "([^\\/,]+)\\/[^\\/,]+";
Matcher matcher = Pattern.compile(reg).matcher(str);
while(matcher.find()){
System.out.println(matcher.group(1));
}
我也是这么做的,我的是这样的
我的str是:(作 者,著,译,社,出版时间,字 数,版 次,1,页 数,381,印刷时间,开 本,16开,印 次,纸 张,n,包 装,平装,定价,当当价,折扣)/OTHER,人民邮电出版社/PUBLISHER,((美)卡登海德,(美)勒海,袁国忠,张劼)/AUTHOR,¥36.00/PRICE1,80折/DISCOUNT,¥45.00/PRICE,9787115191168/ISBN,2009-2-1/DATE结果是:
折扣)
人民邮电出版社
张劼)
¥36.00
80折
¥45.00
9787115191168
2009-2-1
这里作者只有一个 张劼)是什么原因呢?
我也是这么做的,我的是这样的
我的str是:(作 者,著,译,社,出版时间,字 数,版 次,1,页 数,381,印刷时间,开 本,16开,印 次,纸 张,n,包 装,平装,定价,当当价,折扣)/OTHER,人民邮电出版社/PUBLISHER,((美)卡登海德,(美)勒海,袁国忠,张劼)/AUTHOR,¥36.00/PRICE1,80折/DISCOUNT,¥45.00/PRICE,9787115191168/ISBN,2009-2-1/DATE 结果是:
折扣)
人民邮电出版社
张劼)
¥36.00
80折
¥45.00
9787115191168
2009-2-1
这里作者只有一个 张劼)是什么原因呢?
String reg = "(([^\\/]+)|([^\\/,]+))\\/[^\\/,]+";
Matcher matcher = Pattern.compile(reg).matcher(str);
while(matcher.find()){
System.out.println(matcher.group(1).replaceAll("^,*(.*?),*$", "$1"));
}
看来得找本正则表达式的书系统的看看才对,
感谢sd5816690 一直这么耐心的回答,非常感谢!
也谢谢micsolaris,hoho……
如:
(作者一,作者二)/author,9878976688/ISBN,(测试1,测试)/p,(价格1,价格2)/price,(人民邮电出版社,长沙出版社)/publisher,(数据,结构)/title
那样的话可能用途广一些.
期待你的回答.
if (line.Contains("<a href=" + '"' + "http:") && line.Contains("</a>"))
{
Match mc = Regex.Match(line, @"<a href=(.+?)</a>");
while (mc.Success)
{
Match mt = Regex.Match(mc.Groups[1].ToString(), @"(.+?)target=_blank>");
if (mt.Success)
{
string key = mt.Groups[1].ToString().Trim().TrimStart('"').TrimEnd('"');
string str = mc.Groups[1].ToString();
LinkInfo link = new LinkInfo(str.Replace(mt.Value, ""), key);
if (!links.Contains(link))
{
links.Add(link);
}
}
mc = mc.NextMatch();
}
}