7

I need regex that will allow only Latin characters, digits and all other symbols(but not whitespace)

thanks!

UPDATE:

private boolean loginPassHasCorrectSymbols(String input){
        if (input.matches("[A-Za-z0-9\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\>\=\?\@\[\]\{\}\\\^\_\`\~]+$")){
            return true;
        }
        return false;
    }
Stan Malcolm
  • 2,740
  • 5
  • 32
  • 53

5 Answers5

6

I hope I got them all.

"[A-Za-z0-9\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\>\=\?\@\[\]\{\}\\\\\^\_\`\~]+$"

Edit: I forgot that in Java, the regexes are also strings, so you need to actually escape each \ given in the string using another \. I hope I didn't miss any now.

"[A-Za-z0-9\\!\\\"\\#\\$\\%\\&\\'\\(\\)\\*\\+\\,\\-\\.\\/\\:\\;\\<\\>\\=\\?\\@\\[\\]\\{\\}\\\\\\^\\_\\`\\~]+$"
BrockLee
  • 931
  • 2
  • 9
  • 24
  • @LeoCHan: You know Python re better I guess, right? You do not need `$`. – Wiktor Stribiżew Sep 25 '15 at 18:56
  • @stribizhev I imagine I'm fairly decent in both Java and Python. Java was my first after all :) But in the original post, the OP had a `$` at the end of his original regex which made me assume he wanted a `$` to mark the end of a line. – BrockLee Sep 25 '15 at 18:58
  • @LudoBagman I just added more backslashes hoping I escaped everything correctly. – BrockLee Sep 25 '15 at 19:04
  • without symbol " here \\!\\" - it works ) how can I add this symbol? – Stan Malcolm Sep 25 '15 at 19:15
  • 1
    Just a shot in the dark, but check if adding more backslashes will work, making it `\\!\\\"`. So for the `\\\"`, you have 2 backslashes to get one `\\` and another backslash to get `"`. If it works, tell me so I can update my answer please. – BrockLee Sep 25 '15 at 19:23
  • The regex is incorrect – Jorgesys Feb 22 '23 at 17:26
2

How about everything not a whitespace?

"^\S+$"
Shawn Mehan
  • 4,513
  • 9
  • 31
  • 51
  • nope ) cos, i need in such case protect cyrillic symbols ) – Stan Malcolm Sep 25 '15 at 18:26
  • Hmm, Cyrillic wasn't part of the requirement as I grokked it. I have no experience with that but knowledge thereof :). You might get something out of [this](http://stackoverflow.com/questions/1716609/how-to-match-cyrillic-characters-with-a-regular-expression) but I don't even know how to test it myself .... Good luck. – Shawn Mehan Sep 25 '15 at 19:07
0

I did this and it works for me .

Either you can block whitespace by mentioning it on Edittext, or you can block on editetext.addtextChangeListner too by pragmatically .

1>

android:digits="0,1,2,3,4,5,6,7,8,9,*,qwertzuiopasdfghjklyxcvbnm,_,-" 

2>

etNewPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if (etNewPassword.getText().toString().contains(" ")) {
                    etNewPassword.setText(etNewPassword.getText().toString().replace(" ", ""));
                    int iLength = etNewPassword.getText().toString().length();
                    etNewPassword.setSelection(iLength);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

Let me know if any concern.

Tarit Ray
  • 944
  • 12
  • 24
0

For find any symbol except whitespace, you can use this code. I hope you find it useful

public static boolean hasAnySymbolExceptWhitespace(String string){
    return Pattern.matches("(?=.*[^a-zA-Z0-9] ).*", string);
}
fvaldivia
  • 446
  • 5
  • 13
0

Smooth Kotlin solution which allows English letters with some default symbols. Cyrillic or any other language symbols won't be allowed.

//Allows english with usual symbols 
private fun hasNonAllowedSymbols(input: String) : Boolean {
    val regex = "[a-zA-Z0-9\\s-#,/~`'!@$%^&*()_+={}|;<>.?:\"\\[\\]\\\\]*"
    val pattern = Pattern.compile(regex)
    return !pattern.matcher(input).matches()
}
Andrey Kijonok
  • 291
  • 2
  • 17