0

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. But I am not too familar with regex. Maybe you girls/guys could help me out.

My string looks like this:

PING :sendak.freenode.net

Or like this

:[email protected] 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:

:[email protected] PRIVMSG #channelname :test

should be parsed to:

prefix=[[email protected]] 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.

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().

Thanks for any help!

13
  • 2
    And what exactly are you trying to parse out? What have you attempted or tried? Commented May 19, 2014 at 21:58
  • There are many solutions for your problem, but here is a possible one: Use the Splitter class, from the guava library. You should try splitting one line at a time. Commented May 19, 2014 at 21:59
  • How is split() any more complicated than regex when split() takes a regex? Commented May 19, 2014 at 22:00
  • I don't want to cut something 'out'. Above I have 2 example messages and the line below it is how it should get parsed. My current attempts are poor and not working. I basically started a to split at the ":" then splited the second last element of the resulting array at the spaces. And then I stopped because I ran into the issue with the : in the beginning of a line. Commented May 19, 2014 at 22:03
  • 1
    @c-pid Sounds almost like you could use a parser more than regex. Might be easier than working out corner cases, but I don't know... Commented May 19, 2014 at 22:05

1 Answer 1

1

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(":[email protected] PRIVMSG #channelname :test"));
        System.out.println(new Message("[email protected] PRIVMSG #channelname :test"));
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.