简体   繁体   English

拆分不带空格的字符串

[英]Splitting a String without spaces

I have the following string which is generated by an external program (OpenVAS) and returned to my program successfully as a string. 我有以下字符串,它由外部程序(OpenVAS)生成并作为字符串成功返回到我的程序。

<create_target_response id="b4c8de55-94d8-4e08-b20e-955f97a714f1" status_text="OK, resource created" status="201"></create_target_response>

I am trying to split the string to give me the "b4c8d....14f1" without the inverted commas. 我试图拆分字符串给我“b4c8d .... 14f1”没有引号。 I have tried all sorts of escape methods and keep getting the else method "String does not contain a Target ID". 我已经尝试了各种各样的转义方法,并继续获取else方法“字符串不包含目标ID”。 I have tried removing the IF statement checking for the string, but continue to have the same issue. 我已经尝试删除IF语句检查字符串,但继续遇到相同的问题。 The goal is to get my id string into jTextField6. 目标是将我的id字符串放入jTextField6。 String Lob contains the full string as above. String Lob包含如上所示的完整字符串。

     if (Lob.contains("id=\"")){
      // put the split here
           String[] parts = Lob.split("id=\"");
           String cut1 = parts[1];

           String[] part2 = cut1.split("\"");
           String TaskFinal = part2[0];

           jTextField6.setText(TaskFinal);

      }
       else {
           throw new IllegalArgumentException("String does not contain a Target ID");
       }

  } catch (IOException e) {
     e.printStackTrace();
  }

It seems I only need to escape the " and not the = (Java kicks up an error if i do) 似乎我只需要逃避“而不是=(如果我这样做,Java就会出错)

Thanks in advance 提前致谢

EDIT: Code as it stands now using jSoup lib - The 'id' string won't display. 编辑:现在使用jSoup lib的代码 - 'id'字符串将不会显示。 Any ideas? 有任何想法吗? Thanks 谢谢

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // TODO add your handling code here:
          String TargIP = jTextField1.getText(); // Get IP Address
          String TargName = jTextField5.getText(); // Get Target Name
          String Vag = "8d32ad99-ac84-4fdc-b196-2b379f861def";
          String Lob = "";

  final String dosCommand = "cmd /c omp -u admin -w admin --xml=\"<create_target><name>" + TargName + "</name><hosts>" + TargIP + "</hosts></create_target>\"";
  3</comment><config id='daba56c8-73ec-11df-a475-002264764cea'/><target id='" + Vag + "'/></create_task>\"";
  final String location = "C:\\";
  try {
     final Process process = Runtime.getRuntime().exec(
        dosCommand + " " + location);
     final InputStream in = process.getInputStream();
     int ch;
     while((ch = in.read()) != -1) {
        System.out.print((char)ch);
       Lob = String.valueOf((char)ch);
       jTextArea2.append(Lob);


     }

  } catch (IOException e) {
     e.printStackTrace();
  }
           String id = Jsoup.parse(Lob).getAllElements().attr("id");
     System.out.println(id); // This doesn't output? 
}

This will get you your job id more easily: 这样可以更轻松地获得您的工作ID:

int idStart = Lob.indexOf("id=")+("id=\"").length();
System.out.println(Lob.substring(idStart,Lob.indexOf("\"",idStart)));

Split on the basis of ". You can get all the key values. 在“。”的基础上拆分。您可以获得所有关键值。

String str = "<create_target_response id=\"b4c8de55-94d8-4e08-b20e-955f97a714f1\" status_text=\"OK, resource created\" status=\"201\"></create_target_response>";
String[] tokens = str.split("\\\"");
System.out.println(tokens[1]);
System.out.println(tokens[5]);

output: 输出:

b4c8de55-94d8-4e08-b20e-955f97a714f1
201

This is a lot simpler than you're making it, to my mind. 在我看来,这比制作它简单得多。 First, split on space , then check if an = is present. 首先,拆分space ,然后检查是否存在= If it is, split on the = , and finally remove the " from the second token. 如果是,则在=split ,最后从第二个令牌中删除"

The tricky bit is the spaces inside of the "" . 棘手的一点是""内部的空间。 This will require some regular expressions, which you can work out from this question . 这将需要一些正则表达式,您可以从这个问题中解决

Example

String input; // Assume this contains the whole string.
String pattern; // Have fun working out the regex.

String[] values = input.split(pattern);

for(String value : values)
{
    if(value.contains("=")) {
        String[] pair = value.split("=");
        String key = pair[0];
        String value = pair[1].replaceAll("\"");

        // Do something with the values.
    }
}

Advantage of my approach 我的方法的优势

Is that provided the input follows the format of key="value" key="value" , you can parse anything that comes through, rather than hard coding the name of the attributes. 如果提供的输入遵循key="value" key="value"的格式,您可以解析通过的任何内容,而不是硬编码属性的名称。

And if this is XML.. 如果这是XML ..

Then use an XML parser. 然后使用XML解析器。 There is a good (awesome) answer that explains why you shouldn't be using String manipulation to parse XML/HTML. 有一个很好的(很棒的)答案可以解释为什么你不应该使用String操作来解析XML / HTML。 Here is the answer. 是答案。

You can use a regex to extract what is needed; 您可以使用正则表达式来提取所需内容; what is more, it looks like the value of id is a UUID. 更重要的是,看起来id的值是UUID。 Therefore: 因此:

private static final Pattern PATTERN
    = Pattern.compile("\\bid=\"([^\"]+)\"");

// In code...
public String getId(final String input)
{
    final Matcher m = PATTERN.matcher(input);
    if (!m.find())
        throw new IllegalArgumentException("String does not contain a Target ID");
    final String uuid = m.group(1);
    try {
        UUID.fromString(uuid);

    } catch (IllegalArgumentException ignored) {
        throw new IllegalArgumentException("String does not contain a Target ID");
    }

    return uuid;
}

Everyone's telling you to use an XML parser (and they're right) but noone's showing you how. 每个人都告诉你使用XML解析器(他们是对的),但没有人告诉你如何。

Here goes: 开始:

String lob = ...

Using Jsoup from http://jsoup.org , actually an HTML parser but also handles XML neatly: 使用来自http://jsoup.org的 Jsoup,实际上是一个HTML解析器,但也整齐地处理XML:

String id = Jsoup.parse(lob).getAllElements().attr("id");
// b4c8de55-94d8-4e08-b20e-955f97a714f1

With built-in Java XML APIs, less concise but no addtional libraries: 使用内置的Java XML API,不那么简洁但没有附加库:

Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder()
    .parse(new InputSource(new StringReader(lob)));
String id = dom.getDocumentElement().getAttribute("id");
// b4c8de55-94d8-4e08-b20e-955f97a714f1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM