以特殊方式拆分字符串

时间:2012-02-22 13:47:10

标签: java regex string split

我有这个字符串:"player.login name=username;x=52;y=406"我怎么能分开它以便我可以轻松地Player pl = new Player(name, x, y)

我尝试了一个看起来像这样的正则表达式:"([a-zA-Z_]+)[=]{1}([a-zA-Z0-9_]+)[;]{1}"但是我不是很擅长正则表达式所以它不起作用。

编辑:有人提出了一个很好的解决方案,因此无需发表评论。 :)

我用过的东西:

public static void main(String args[]) {
    String login = "player.login name=username;x=52;y=406";
    String str = login.substring("player.login".length() + 1);
    String[] sp = str.split(";");
    Player player = new Player("", 0, 0);
    for (String s : sp) {
        String[] a = s.split("=");
        if (a[0].equals("name")) player.username = a[1];
        else if (a[0].equals("x")) player.x = toInt(a[1]);
        else if (a[0].equals("y")) player.y = toInt(a[1]);
    }
    System.out.println("Player: " + player.username + " @ " + player.x + ", " + player.y);
}

public static int toInt(String s) {
    return Integer.parseInt(s);
}

5 个答案:

答案 0 :(得分:3)

这应该有用(你应该在调用exp.split("=")[1]之前添加绑定的检查):

public static void main(String[] args) {
    String s = "player.login name=username;x=52;y=406";
    String[] expressions = s.split(";");
    for (String exp : expressions) {
        System.out.println(exp.split("=")[1]);
    }
}

答案 1 :(得分:2)

由于Java 7支持命名捕获组,因此这将是一个不错的用法

String s = "player.login name=username;x=52;y=406";

Pattern p = Pattern.compile("name=(?<UserName>[^;]+);" +  // Match the UserName in the Named Group "UserName", matching at least one non semicolon
    "x=(?<x>\\d+);" + // Match the value of x in the Named Group "x", matching at least one digit
    "y=(?<y>\\d+)"    // Match the value of y in the Named Group "y", matching at least one digit
    );
Matcher m = p.matcher(s);
if (m.find()) {
    System.out.println(m.group("UserName"));
    System.out.println(m.group("x"));
    System.out.println(m.group("y"));
}

答案 2 :(得分:1)

你可以使用String.split()三次,分隔一个空格,然后是分号,然后是=。或者 assylia 的答案是通过两个分割来实现的 - 最佳解决方案取决于您是否要验证要丢弃的文本部分。

或使用正则表达式...

String regex = ".+=(\\w+);x=(\\d+);y=(\\d+)"

...您可以使用以下代码运行:

    Pattern p = Pattern.compile(".+=(\\w+);x=(\\d+);y=(\\d+)");
    Matcher m = p.matcher("player.login name=username;x=52;y=406");
    m.find();
    System.out.println(m.group(1) + "," + m.group(2) + "," + m.group(3));

答案 3 :(得分:1)

或者您可以尝试使用番石榴:

String data = "player.login name=username;x=52;y=406";
List<String> fields = ImmutableList.copyOf(Splitter.on(' ').limit(2).split(data));

String type = fields.get(0);
Map<String, String> properties = Splitter.on(';').trimResults().withKeyValueSeparator("=").split(fields.get(1));

答案 4 :(得分:0)

尝试使用模式:

public static void main(String args[]){
    String str = "player.login name=username;x=52;y=406";

    Pattern p = Pattern.compile("^player\\.login name=(.+);x=(\\d+);y=(\\d+)$");

    Matcher m = p.matcher(str);

    Player player;
    if (m.matches()){
        String name = m.group(1);
        int x = Integer.parseInt(m.group(2));
        int y = Integer.parseInt(m.group(3));

        System.out.println(name);
        System.out.println(x);
        System.out.println(y);

        player = new Player(name, x, y);
    } else {
        player = null;
    }

    // Do stuff with player.
}