简体   繁体   English

Java-如何使用RegEx解析此字符串

[英]Java - How to parse this String using RegEx

So I have this string and I want to parse it. 所以我有这个字符串,我想解析它。 Normally I would use string.split() for it but it's a bit complicated so I thought that may using regex is better in this case. 通常我会使用string.split(),但是它有点复杂,所以我认为在这种情况下使用regex会更好。 But I am not too familar with regex. 但是我对regex不太熟悉。 Maybe you girls/guys could help me out. 也许你们的女孩/家伙可以帮助我。

My string looks like this: 我的字符串如下所示:

PING :sendak.freenode.net

Or like this 或者像这样

:username!~user@hostname.tld PRIVMSG #channelname :test

And should be parsed into it's components prefix, username, command, channel, text. 并且应解析为其组件的前缀,用户名,命令,通道,文本。

Example: 例:

PING :sendak.freenode.net 

Should be: 应该:

prefix=[] username=[] command=[PING] channel=[] text=[sendak.freenode.net]

and the string: 和字符串:

:username!~user@hostname.tld PRIVMSG #channelname :test

should be parsed to: 应该解析为:

prefix=[username!~user@hostname.tld] username=[username] command=[PRIVMSG] channel=[#channelname] text=[test]

In the end I have to fill out these variables: 最后,我必须填写以下变量:

message.prefix = "";
message.username = "";
message.command = "";
message.channel = "";
message.text = "";

I am spliting a line at a time! 我一次要分线!

Fairly obvious that it's gonna be a small IRC chat. 很明显,这将是一个小型IRC聊天。

The problem I expierence is that it can start with a ":" but does not have to.Thus making it fairly complex to realising using several splits(). 我遇到的问题是,它可以以“:”开头,而不必一定是这样。因此,使用多个splits()使其变得相当复杂。

Thanks for any help! 谢谢你的帮助!

I think this regex may help you: "(:?((. )![^ ] ))? ?([^ ] ) (#([^ ] ) )?(:(.*))?" 我认为此正则表达式可以为您提供帮助: “((??((。 )![^] ))??([^] )(#([^] ))?(:(。*))?” :

import java.util.regex.Matcher;
import java.util.regex.Pattern;

class Message{
    public String prefix = "";
    public String userName = "";
    public String command = "";
    public String channel = "";
    public String text = "";

    public Message(String line){
        Matcher matcher = Pattern.compile("(:?((.*)![^ ]*))? ?([^ ]*) (#([^ ]*) )?(:(.*))?").matcher(line);
        if (matcher.matches()){
            prefix = matcher.group(2) != null? matcher.group(2): "";
            userName = matcher.group(3) != null? matcher.group(3): "";
            command = matcher.group(4) != null? matcher.group(4): "";
            channel = matcher.group(6) != null? matcher.group(6): "";
            text = matcher.group(8) != null? matcher.group(8): "";
        }
    }

    @Override
    public String toString() {
        return String.format("prefix=[%s] username=[%s] command=[%s] channel=[%s] text=[%s]", prefix, userName, command, channel, text);
    }
}


public class TestRegex {

    public static void main(String[] args) {
        System.out.println(new Message("PING :sendak.freenode.net"));
        System.out.println(new Message(":username!~user@hostname.tld PRIVMSG #channelname :test"));
        System.out.println(new Message("username!~user@hostname.tld PRIVMSG #channelname :test"));
    }
}

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

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