CodeKingPlusPlus
CodeKingPlusPlus

Reputation: 16081

Java Regular Expressions with whitespace

I am trying to match a pattern like so:

TR @(any number of word characters go here):

So the pattern begins with TR then has one space then has @ and then has any characters and terminates with a :

Here is my regex: Pattern p = Pattern.compile("TR\\s@[\\w]+:");

It is working and for example will fail on:

TR @abcnews:

I think my error is with the whitespace.

Upvotes: 0

Views: 169

Answers (1)

Javier Diaz
Javier Diaz

Reputation: 1830

DEMO

Regex: TR\s+@(\w+):

This even backreferences the text, it accepts multiple spaces between the TR and @ so it would work pretty well for you, sir.

EDIT A java code that it's properly working:

Matcher ma = Pattern.compile("TR\\s+@(\\w+):").matcher("TR    @asdfasd:");
while (ma.find()) {
    System.out.println(ma.group(1));
}

Upvotes: 1

Related Questions