Java regex - expression with exactly one whitespace

后端 未结 6 1998
攒了一身酷
攒了一身酷 2021-01-14 12:08

I want to match all expressions with exactly one whitespace. Currently, I\'m using [^\\\\s]*\\\\s[^\\\\s]*. That doesn\'t seem like a very good way, though.

6条回答
  •  佛祖请我去吃肉
    2021-01-14 12:59

    Use transliterate. It has to be an independent test, the regex you have above cannot be combined with a larger regex and still test for a single whitespace.

    Transliterate is 10-20 times faster than a regex for this test.
    This is a jtr example:

    String aInput = "This is a test, 123.";
    CharacterReplacer cReplacer = Perl5Parser.makeReplacer( "tr[ \\t\\r\\n\\f\\x0B][ \\t\\r\\n\\f\\x0B]" );
    String aResult = cReplacer.doReplacement( aInput );
    int nMatches = cReplacer.getMatches();
    
    if (nMatches == 1) { ... }
    

提交回复
热议问题