Understanding useDelimiter in Scanner : why I get blank token?

前端 未结 2 496
無奈伤痛
無奈伤痛 2021-01-19 07:20

I\'m using scanner with delimiter and I\'ve came across a strange behaviour I\'d like to understand.

I\'m using this programm :

    Scanner sc = new          


        
相关标签:
2条回答
  • 2021-01-19 07:44

    I have a feeling that you are causing two delimiter captures in places where there's a blank space followed by punctuation. Why not simply use [\\s\\p{Punct}]+?

    This regex \\s+|\\p{Punct}+ will first capture the empty space and swallow it, then will capture the next delimiter as the punctuation. That will be two delimiters next to each other with nothing in between (the empty token).

    0 讨论(0)
  • 2021-01-19 07:46

    I happened to encounter the empty token problem with the Scanner class too. I think the delimiter pattern has to be made greedy by surrounding it with parenthesis and appending + to the group. The pattern I used looks like this

    "((\\s)+|(\\\\r\\\\n)+|\\p{Punct}+)+". 
    
    0 讨论(0)
提交回复
热议问题