仅使用第一个java实例拆分字符串

仅使用第一个java实例拆分字符串,java,arrays,regex,string,Java,Arrays,Regex,String,我有来自某个telnet服务器的以下响应 String response = "I:BARCO@noiclt22815||K:CMS||O:REgetPerspectiveList||A0:googleP||A1:yahooP||A2:gmail||A3:test||A4:hello||A16:CCTV Barco||A17:CCTV: Corridor CC||A18:CCTV: DR Andy Warhol||A19:CCTV: DR Gaudi (Analog)||A20:CCTV: DR

我有来自某个telnet服务器的以下响应

String response = "I:BARCO@noiclt22815||K:CMS||O:REgetPerspectiveList||A0:googleP||A1:yahooP||A2:gmail||A3:test||A4:hello||A16:CCTV Barco||A17:CCTV: Corridor CC||A18:CCTV: DR Andy Warhol||A19:CCTV: DR Gaudi (Analog)||A20:CCTV: DR Miro||A21:CCTV: Entrance CC||A22:CCTV: Gaudi Demo Room Megapixel||";
我想得到属性值,例如A0,A1等,因此我写了下面的逻辑

String[] strings = response.split("[||]");
List<String> list = new ArrayList<>();

for (String string : strings) {
    if (string.contains(":")) {
        String[] attributes = string.split(":");
        if (attributes[0].startsWith("A")) {
            list.add(attributes[1]);
        }
    }
}
String[]strings=response.split([| |]]);
列表=新的ArrayList();
for(字符串:字符串){
if(string.contains(“:”){
String[]attributes=String.split(“:”);
if(属性[0].startsWith(“A”)){
添加(属性[1]);
}
}
}
但我的问题是string.split(“:”)split提供了字符串数组,但我只需要两个长度大小的字符串数组。例如,响应A17属性将“CCTV”作为属性[1],将“走廊CC”作为属性[2],但我仅要求将“CCTV:走廊CC”作为属性[1]

我应该在string.split(regexp)中编写什么样的正则表达式,以便只使用两个长度大小的字符串数组,根据冒号运算符的第一个实例分割字符串

在您的拆分中

String[] attributes = string.split(":", 2);
这将强制只应用一次正则表达式(根据文档,它应用了n-1次),因此您的分组将只在冒号字符上拆分一次

一旦应用到代码中,结果如下:

[googleP, yahooP, gmail, test, hello, CCTV Barco, CCTV: Corridor CC, CCTV: DR Andy Warhol, CCTV: DR Gaudi (Analog), CCTV: DR Miro, CCTV: Entrance CC, CCTV: Gaudi Demo Room Megapixel]

下面的代码首先在管道上拆分,然后使用正则表达式提取属性和属性。请注意,即使在那里,您也可能通过进行另一次拆分而逃脱处罚

String response = "I:BARCO@noiclt22815||K:CMS||O:REgetPerspectiveList||A0:googleP||A1:yahooP||A2:gmail||A3:test||A4:hello||A16:CCTV Barco||A17:CCTV: Corridor CC||A18:CCTV: DR Andy Warhol||A19:CCTV: DR Gaudi (Analog)||A20:CCTV: DR Miro||A21:CCTV: Entrance CC||A22:CCTV: Gaudi Demo Room Megapixel||";
String[] metaParts = response.split("\\|\\|");

for (int i=0; i < metaParts.length; ++i) {
    String property = metaParts[i].replaceAll("(.*):(.*)", "$1");
    String attribute = metaParts[i].replaceAll("(.*):(.*)", "$2");
    System.out.println(property + ":" + attribute);
}
String response=“I:BARCO@noiclt22815||答:1:Ya箍(甲:亚甲:亚甲:亚甲:亚甲:亚甲:Ya箍(甲甲:亚甲:亚甲:亚甲:亚甲:亚甲:亚甲:亚甲:甲:甲:G甲:厘米:CMS:CMS:CMS:CMS:CMS:CMS:CMS:CMS:CMS:CMS:CMS:CMS:CMS:凯凯凯撒:CMS:CMS:::;;;;;;;A:CMA:CMS:CMS:CMS:CMS:CMS:CMS:CMS:CMS::::::::::::;;;;;;;;;;;A:CMS:CMS:CMS:CMS:CMS:CMS:CMS:CMS:CMS:CMS:CMS:CMS:::::::::::::::::::::::::::::::::;
String[]元部件=response.split(“\\\\\\\\\\”);
对于(int i=0;i
正如这里的其他人所说,正则表达式不是解决所有开发问题的灵丹妙药。拆分无疑是解决这个问题的重担。

import java.util.HashMap;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TestRegex {

public static void main(String[] args) {

    String response = "I:BARCO@noiclt22815||K:CMS||O:REgetPerspectiveList||A0:googleP||A1:yahooP||A2:gmail||A3:test||A4:hello||A16:CCTV Barco||A17:CCTV: Corridor CC||A18:CCTV: DR Andy Warhol||A19:CCTV: DR Gaudi (Analog)||A20:CCTV: DR Miro||A21:CCTV: Entrance CC||A22:CCTV: Gaudi Demo Room Megapixel||";
    Matcher mat = Pattern.compile("([^\\:]+)\\:([^\\|]+)\\|\\|").matcher(response);
    HashMap<String, String> hm = new HashMap();
    while( mat.find() ) {
        if(mat.groupCount() > 0) {
            String k = response.substring( mat.start(1), mat.end(1));
            String v = response.substring( mat.start(2), mat.end(2));
            hm.put(k, v);

            System.out.println(k +  " => " + v);   // debug
        }
    }
    // you may refer to your data from hashmap hm here!
    System.out.println(hm.get("K"));
}

} // end class
导入java.util.regex.Matcher; 导入java.util.regex.Pattern; 公共类TestRegex{ 公共静态void main(字符串[]args){ String response=“I:BARCO@noiclt22815||答:1:Ya箍(甲:亚甲:亚甲:亚甲:亚甲:亚甲:Ya箍(甲甲:亚甲:亚甲:亚甲:亚甲:亚甲:亚甲:亚甲:甲:甲:G甲:厘米:CMS:CMS:CMS:CMS:CMS:CMS:CMS:CMS:CMS:CMS:CMS:CMS:CMS:凯凯凯撒:CMS:CMS:::;;;;;;;A:CMA:CMS:CMS:CMS:CMS:CMS:CMS:CMS:CMS::::::::::::;;;;;;;;;;;A:CMS:CMS:CMS:CMS:CMS:CMS:CMS:CMS:CMS:CMS:CMS:CMS:::::::::::::::::::::::::::::::::; Matcher mat=Pattern.compile(“([^\\\:]+)\:([^\\\\\\]+)\\\\\\\\\\\\”)”).Matcher(响应); HashMap hm=新的HashMap(); while(mat.find()){ 如果(mat.groupCount()>0){ 字符串k=响应子字符串(mat.start(1),mat.end(1)); 字符串v=响应子字符串(mat.start(2),mat.end(2)); hm.put(k,v); System.out.println(k+“=>”+v);//调试 } } //您可以在这里参考hashmap hm中的数据! System.out.println(hm.get(“K”)); } }//结束类
您可以使用带有indexOf的substring方法来获取第一个
的索引,然后使用substring直到该索引,并将两个字符串放入数组中。我可以这样做,但我需要regex.Pranav,它给了我和以前一样的回答,意味着CCTV只针对17个属性。这绝对不是那个问题的重复。它真的感觉这是一个很大的工作来完成一些regex可能真的不应该完成的事情。。。