简体   繁体   English

从以“ name:”开头的字符串中获取参数

[英]Get argument from string starting with “name:”

I have: 我有:

String query = IRON_SWORD 1 name:&aMagic_Iron_Sword lore:&5Magic|is|amazing sharpness:10 hide_enchants

I split it into args: 我将其拆分为args:

String[] args = s.split(" ");

Now how do I get the argument starting with name: Note, I don't mean getting it with args[2] because the name could be the 5th or 4th arg. 现在如何获取以name:开头的参数name: 注意,我的意思不是以args[2]开头, 因为名称可以是第5或第4个arg。

The regex \\sname:(\\S*) will successfully find it in the larger string (without the split call). 正则表达式\\sname:(\\S*)将在较大的字符串中成功找到它(无需split调用)。

Here's sample code: 这是示例代码:

    String s = "IRON_SWORD 1 nme:&aMagic_Iron_Sword lore:&5Magic|is|amazing sharpness:10 hide_enchants";
    Pattern p = Pattern.compile("\\sname:(\\S*)");
    Matcher m = p.matcher(s);
    if (m.find())
    {
        String name = m.group(1);
    }

You may use regex. 您可以使用正则表达式。

Pattern p = Pattern.compile("(?<=\\bname:)\\S+");
Matcher m = p.matcher(s);
while(m.find())
{
System.out.println(m.group());
}

What about trying: 尝试一下:

foreach args as find
{
    searched = split (find, ":")
    if searched[0] == 'name' 
    {
         result = searched[1]
    }
}

I dont know what language you are using but I am sure you could adapt. 我不知道您使用的是哪种语言,但我相信您会适应。

Three approaches come to mind: 我想到了三种方法:

  1. Search your split results for the element that startsWith() "name", then split that and get the element at index 1. 在拆分结果中搜索startsWith() “ name”的元素,然后拆分并在索引1处获取该元素。
  2. Utilize String.indexOf() and String.substring() to exact what you're looking for 利用String.indexOf()String.substring()精确找到您要的内容
  3. Use regex pattern "name:(\\\\S*) " and capture the data you want into group 1 使用regex模式"name:(\\\\S*) "并将所需的数据捕获到组1中

Examples of these approaches: 这些方法的示例:

public static void main(String[] args) throws Exception {
    String query = "IRON_SWORD 1 name:&aMagic_Iron_Sword lore:&5Magic|is|amazing sharpness:10 hide_enchants";

    System.out.println(getNameSplit(query));
    System.out.println(getNameSubstring(query));
    System.out.println(getNameRegex(query));
}

public static String getNameSplit(String query) {
    String[] pieces = query.split(" ");
    for (String piece : pieces) {
        if (piece.startsWith("name")) {
            return piece.split(":")[1];
        }
    }
    return null;
}

public static String getNameSubstring(String query) {
    int nameIndex = query.indexOf("name:");
    int spaceIndexAfterName = query.indexOf(" ", nameIndex);
    // +5 to skip name:
    return query.substring(nameIndex + 5, spaceIndexAfterName);
}

public static String getNameRegex(String query) {
    Matcher matcher = Pattern.compile("name:(\\S*) ").matcher(query);
    if (matcher.find()) {
        return matcher.group(1);
    }
    return null;
}

Results: 结果:

&aMagic_Iron_Sword
&aMagic_Iron_Sword
&aMagic_Iron_Sword

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

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