简体   繁体   中英

java pattern matching insenitive to spaces

I have:

Pattern pat = Pattern.compile("(\\d+) (\\d+) (1$)");
Matcher mat = pat.matcher(line);

with matches for:

1 2 1

but not for:

1     2     1

How can I achieve, that pattern matching is insenitive according to spaces between numbers?

Use \\s for one space and add a + that means one ore more spaces.

"(\\d+)\\s+(\\d+)\\s+(1$)"

If you want zero or more spaces you have to use a * instead of the + .

Use quantifier (+) with a space, to match one or more space: -

Pattern.compile("(\\d+)\\s+(\\d+)\\s+(1$)");

Similarly there are other quantifiers : -

  • * to match 0 or more
  • ? to match 0 or 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM