Java uses regexp to split space?

I try to match and capture commands and parameters from the following inputs:

!command param1 param2

I am using the Java classes pattern and matcher:

private Pattern regExp = Pattern.compile(
        "^!(?<command>[^\\s]*)((?:\\s+)(?<param>[^\\s]*))*$");

public String command() {
    m = regExp.matcher(getMsg());
    return m.matches() ? m.group("command") : "";
}

public String param(int index) {
    return m.group(index);
}

Use this, too( http://fiddle.re/yanta6 )To experiment

Some pointers and help are appreciated!

Solution

Personally, I don't use regular expressions If your input is

!command param1 param2 paramX

Then normal string operation can do a good job Just discard the opening! Then use split on ''

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>