Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 以特殊的方式拆分字符串_Java_Regex_String_Split - Fatal编程技术网

Java 以特殊的方式拆分字符串

Java 以特殊的方式拆分字符串,java,regex,string,split,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[]) {

我有这样一个字符串:
“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);
}
这应该可以工作(在调用
exp.split(“=”[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));

尝试改用模式:

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.
}

或者你可以试试番石榴:

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));
String data=“player.login name=username;x=52;y=406”;
列表字段=ImmutableList.copyOf(Splitter.on(“”).limit(2).split(数据));
字符串类型=字段。获取(0);
映射属性=Splitter.on(“;”).trimResults().withKeyValueSeparator(“=”).split(fields.get(1));

因为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"));
}
String s=“player.login name=username;x=52;y=406”;
Pattern p=Pattern.compile(“name=(?[^;]+);”+//匹配命名组“UserName”中的用户名,至少匹配一个非分号
“x=(?\\d+)”+//匹配命名组“x”中x的值,至少匹配一个数字
“y=(?\\d+)//匹配命名组“y”中y的值,至少匹配一个数字
);
匹配器m=匹配器p;
if(m.find()){
System.out.println(m.group(“用户名”));
系统输出打印LN(m.group(“x”));
系统输出打印LN(m.group(“y”));
}

为什么不使用分隔符为``和
的StringTokenizer
然后在
=
符号上的第一个令牌之后拆分每个令牌?谢谢,它很有效,并且是最适合我需要的令牌。如果您觉得这是您问题的答案,您可以接受它()
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"));
}