javaregexwhitespace

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 which 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!


Solution

  • 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.