btrballin
btrballin

Reputation: 1464

How to set delimiter as "\\p{Punct}" excluding quotation mark?

If I want to set the scanner's delimiter to scanner.useDelimiter("\\p{Punct}"); but don't want the quotation mark to be included in that list, is there a simple way to exclude that?

I tried to do s.useDelimiter("(\\p{Digit}|\\s|\\p{Punct}&&[^"])+"); but the quotation mark that is in the bracket is closing the for quotation mark.

Upvotes: 1

Views: 615

Answers (2)

M A
M A

Reputation: 72884

You can call the method Scanner#useDelimiter(Pattern):

scanner.useDelimiter(Pattern.compile("[\\p{Punct}&&[^\"]]"))

[[\\p{Punct}&&[^\"]] matches all characters covered by \\p{Punct} except the double quote which has been escaped.

Upvotes: 3

user2864740
user2864740

Reputation: 61975

This is called Character Class Subtraction, see Java Trail: [Regular Expression] Character Classes:

Finally, you can use subtraction to negate one or more nested character classes, such as [0-9&&[^345]]. This example creates a single character class that matches everything from 0 to 9, except the numbers 3, 4, and 5.

For the given request this is the pattern [\p{Punct}&&[^"]] (escape for a string literal, as normal).

Upvotes: 1

Related Questions