user882347
user882347

Reputation:

Grabbing an IRC channel in a string with regex

This is my first time posting here, but I've found a lot of answers here, which is awesome.

I'm currently trying to grab an IRC channel from a string, and the IRC channel prefix is (#), which means that the channel would be something like:

Channels don't have spaces, so if it was (#a channel), the channel would just be (#a).

I'm trying to parse that sort of like I parsed my URLs, which is like so:

public String[] splitString(String string) {
    String pattern = "(?i)(http(s?)://|www\\.)\\S++";
    String[] split = string.split(String.format("((?<=%s)\\s++)|(\\s++(?=%s))", pattern, pattern));
    return split;
}

Leaving me with a result that would split a String in a way that there is separate parts. An example of how it works is:

String test = "let's see how this http://google.com/ thing works, www.shall.we";
Result: ["let's see how this", "http://google.com/", "thing works, ", "www.shall.we"]

I hope that makes sense.

All in all, I guess I'm just looking for some regex to get an IRC channel from a string.

I've found this Match IRC Channel with regular expression but it doesn't seem to work how I'm looking for it to work.

Upvotes: 1

Views: 278

Answers (1)

Stephan
Stephan

Reputation: 1190

The regex to do this for your simple definition would be

(#\S+)

which means a # followed by at least one character that is not a whitespace. If you want, you can narrow the allowed characters by choosing another character class like \w to allow only letters and digits.

Upvotes: 1

Related Questions