Daniel Stark
Daniel Stark

Reputation: 47

How to express several whitespaces in a regex?

My question goes as the title says: is there any way to "tell" a regex there's a big blank between two fields of s string? With big I mean trailing whitespaces, or tabs, or any combination of both. The thing is I'm working with strings wich I must extract some fields from. Something like:

label OPCODE operand

The regex I'm using is as follows:

"([a-z]*)(\\s)([A-Z]+)(\\s)([a-z]*)"

The problem is java only recognizes every " \s" character as just one space, and doesn't count for tabs. If anyone can help me I'd really appreciate it! :)

Upvotes: 1

Views: 1917

Answers (1)

RamenChef
RamenChef

Reputation: 5558

The \s metacharacter should account for tabs. What you're missing is the quantifier. You need a + or * quantifier (depending on whether you allow no space between the two segments) to detect any number of whitespaces.

Upvotes: 3

Related Questions