groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Keegan Witt <>
Subject Re: Check a string if there is a special character in it using Groovy
Date Tue, 07 Jul 2015 14:57:39 GMT
Sorry for not replying earlier, this got buried in my inbox.

This isn't really a Groovy-specific thing.  You'd handle the characters the
same as you would if you were in Java.  If you're wanting to allow only
printable ASCII, you might do something like this
static boolean isPrintableAscii(String str) {
    try {
        CharBuffer buff = Charset.forName("US-ASCII").newDecoder()
        for (char c in buff.toString())
            if (Character.getType(c) == Character.CONTROL)
                return false
    } catch(CharacterCodingException e) {
        return false
    return true

If your legal range includes more than ASCII but has other rules for what
to disallow, you can just read as a string without the decoder and add more
rules to the if statement.  But it sounds like you're not allowing most
Unicode characters.  In such cases, I think a whitelist would be preferable
to a gigantic blacklist of all the characters you want to disallow (which
would make adding checks to the if statements or a regex pretty cumbersome).

If you could elaborate more on what characters are legal for your use case,
I can provide a better answer.  Is it only diacritical characters that are
illegal for your use case?  Would, for example, a Chinese character be
legal?  You might want to think through some more what characters you want
to allow/disallow and *why*.


On Sat, Jul 4, 2015 at 3:09 AM, shashank90 <>

> Special Characters like 󗸚𚰀. Am not sure under which category these
> characters fall into. Should we use regex expression for this?
> --
> View this message in context:
> Sent from the Groovy Dev mailing list archive at

View raw message