简体   繁体   English

java如何从指定符号的字符串中获取列表?

[英]java How to get the list from a string by the specified symbol?

example I have a string 例子我有一个字符串

"123<a>3213<b>3434343<c>,example <d><1><2><3>"

I want to get the inner text by the symbol "<>" 我想通过符号"<>"获得内部文本

how can I get the list [a,b,c,d,1,2,3] ??? 我如何获得列表[a,b,c,d,1,2,3] ???

您可以使用StringUtils.substringsBetween(String str,String open,String close)

String[] parts = StringUtils.substringsBetween("123<a>3213<b>3434343<c>,example <d><1><2><3>", "<", ">");

You want the text between <..> . 您需要<..>之间的文本。 Use a Pattern and Matcher over the given String while grouping the text between <..> <..>之间的文本分组时,在给定的String使用PatternMatcher

String text = "123<a>3213<b>3434343<c>,example <d><1><2><3>";
Pattern pattern = Pattern.compile("<(.*?)>"); // reluctant quantifier
Matcher matcher = pattern.matcher(text);
List<String> entries = new LinkedList<>();
while (matcher.find()) 
    entries.add(matcher.group(1)); // group 0 is the whole match, we only want what's between <>
System.out.println(entries);

This prints 此打印

[a, b, c, d, 1, 2, 3]

Untested, hardly bullet proof, little error checking, but it should work. 未经测试,几乎不防弹,几乎没有错误检查,但应该可以。 Of course things could be better, this is just from memory. 当然,情况可能会更好,这仅来自内存。

public List<String> getThings(String source) {
    char[] chars = source.toCharArray();
    boolean capturing = false;
    List<String> result = new ArrayList<String>();
    String token = "";

    for(char c : chars) {
        if (!capturing) {
            if (c == '<') { // found open delimiter, start capture.
                capturing = true;
                continue;
            }
        } else {
            if (c == '>') { // Found closing delimiter, stop capture.
                results.add(token);
                token = "";
                capturing = false;
                continue;
            }
            token = token + c;
        }
    }
    if (!scanning) {
        throw new RuntimeException("Source string ended with missing closing '>'");
    }
    return results;
}

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

相关问题 如何从MongoClient获取连接字符串中指定的Mongo数据库,Java - How to get the Mongo database specified in connection string from MongoClient, Java 如何使用Java 8 Stream从Map列表中的字符串中获取列表? - How to get a List from string from List of Map with Java 8 Stream? 如何从指定的字符串中提取值:java - How to extract value from a specified string : java JAVA:如何进入清单<String> - JAVA : How get to the List<String> 从字符串数组中获取包含指定字符的字符串列表 - Get a list of strings that contain a specified character from an array of string 从数组列表中获取所有以指定字符串开头的元素 - Get all the elements which are starting with a specified string from an Array List 如何检索由指定字符串过滤的Java中的AWS AutoScalingGroups列表? - How to retrieve list of AWS AutoScalingGroups in Java filtered by a specified String? 使用Java从编码的URL参数字符串中获取指定的参数 - Get specified parameter from encoded url paramters String with java 在Java中将字符串分解为句子(在出现指定组的符号之后) - Breaking string into sentences in java (after symbol of specified group occurs) 如何通过使用Java中的split从字符串中获取多个电子邮件的列表? - How to get a list of multiple emails from a string by using split in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM