比如字符串是这样的:
数据/title,结构/title,人民邮电出版社/publisher,9878976688/ISBN,作者一/author,作者二/author……我现在想把同样状态的合并起来,如:
数据结构/title,人民邮电出版社/publisher,9878976688/ISBN,作者一,作者二/author……我用数组做的,但觉得不是很好的方法,大家有没有更好的方法??

解决方案 »

  1.   

    就是有字符串{数据/title,结构/title,人民邮电出版社/publisher,9878976688/ISBN,作者一/author,作者二/author…… }我现在想把同样都是title,publisher,author等状态的合并起来,合并的结果是: 
    {数据结构/title,人民邮电出版社/publisher,9878976688/ISBN,(作者一,作者二)/author……} 
      

  2.   

    用map吧,key和value分别可以放 /title 数据
    遇到key相同的就合并咯
    比如,key都为/title 就数据+结构
      

  3.   


    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
    }
    }可以用正则处理
      

  4.   


    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
    }
    }可以用正则处理
      

  5.   

    用一个类book来表示,title,publisher,author,作为属性类型ArrayList.
      

  6.   

    6楼micsolaris 的我还不太理解呢,得消化一下,呵呵,不过,如果我的message变了,
    比如还有其他的:price,page等状态,你的正则表达式不是还得变么?
      

  7.   

    这没问题的,你可以加上 价格1/price,价格2/price看看,结果还是符合的
      

  8.   

    出现了漏洞,当 出现 价格/price的时候 ,p和publisher会出现问题。但如果英文是不同的话可以正常使用。修改中·~~
      

  9.   


    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(",$", ""));
    }
    }
      

  10.   

    没想到什么好办法,首先用逗号分开,然后再用/分开,然后使用HashMap了.
      

  11.   

    说实话,没看懂 7 楼的方法没用过 (\\3) 这类的表达式(难道是匹配 group(3) 的一部分?)
      

  12.   

    16楼:
    ([^\\/,]+)\\/([^\\/,]+)
    \\/, 表示 / 或者 ,
    [^\\/,]+ 不是 / 或 , 的字符至少出现一次
    ([^\\/,]+) 分组,对应 group(1),后面那个对应 group(2)7楼:
    ^(.*,)?(.*?)/(\\w+)(.*)?,(.*?)/(\\3)(.*)
    ^ 字符串开始
    .* 任意字符出现 0 次或多次
    (.*,)? 前面表达式出现 0 次或 1 次
    \\w 匹配字母、数字、下划线
    \\3 没用过
    这里括号括起来的,同样表示分组 group(x) x=1,2,3,4,5,6,7
      

  13.   

    非常感谢sd5816690详细的注释,改了改你的代码,就实现了我想要的结果,
    再问问,如果得到了这样的结果
    (数据,结构)/title,人民邮电出版社/publisher,9878976688/ISBN,(作者一,作者二,作者三)/author 等等……
    如果写正则表达式提取出(作者一,作者二,作者三),(数据,结构)之类的信息?
    我知道从你的hashtable里可以直接取出,但我想学习一下正则表达式
      

  14.   

    我试了group(1),group(2)之类的,但没有得到我想要的结果
      

  15.   


    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));
    }
      

  16.   


    我也是这么做的,我的是这样的
    我的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
     这里作者只有一个  张劼)是什么原因呢?
      

  17.   


    我也是这么做的,我的是这样的 
    我的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 
    这里作者只有一个  张劼)是什么原因呢?
      

  18.   

    各个数据之间用英文逗号分割,所以一旦内容中出现英文逗号,那么就会被当成另一个内容用下面代码可以得到String str = "(作  者,著,译,社,出版时间,字  数,版  次,1,页  数,381,印刷时间,开  本,16开,印  次,纸  张,n,包  装,平装,定价,当当价,折扣)/OTHER,人民邮电出版社/PUBLISHER,((美)卡登海德,(美)勒海,袁国忠,张劼)/AUTHOR,¥36.00/PRICE1,80折/DISCOUNT,¥45.00/PRICE,9787115191168/ISBN,2009-2-1/DATE";
    String reg = "(([^\\/]+)|([^\\/,]+))\\/[^\\/,]+";
    Matcher matcher = Pattern.compile(reg).matcher(str);
    while(matcher.find()){
    System.out.println(matcher.group(1).replaceAll("^,*(.*?),*$", "$1"));
    }
      

  19.   

    哦 现在明白了,我还以为\\ 表示\ ,唉,字符看的迷迷糊糊的,
    看来得找本正则表达式的书系统的看看才对,
    感谢sd5816690 一直这么耐心的回答,非常感谢!
    也谢谢micsolaris,hoho……
      

  20.   

    16楼,能将处理后的字符按字母排序吗?
    如:
    (作者一,作者二)/author,9878976688/ISBN,(测试1,测试)/p,(价格1,价格2)/price,(人民邮电出版社,长沙出版社)/publisher,(数据,结构)/title
    那样的话可能用途广一些.
    期待你的回答.
      

  21.   

    我今天也碰到了相似的问题,后来用正则表达式解决,相当简单
    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();
                        }
                    }