简体   繁体   English

字符之间的正则表达式空格(Java 开头除外)

[英]Regex spaces between chars except the beginning Java

I've been struggling my mind for the past couple of hours in order to get the following behaviour in a sentence of characters:在过去的几个小时里,我一直在苦苦思索,以便在一个字符的句子中获得以下行为:

John is $@^&%$#@&$^12(random chars...) - match John is $@^&%$#@&$^12(random chars...) - 匹配
John is $@^&%$#@&$^12(random chars...) - match (some spaces at the end) John is $@^&%$#@&$^12(random chars...) - 匹配(末尾有一些空格)
John is $@^&%$#@&$^12(random chars...) - unmatch (some spaces at the beginning) John is $@^&%$#@&$^12(random chars...) - 不匹配(开头有一些空格)

I could not find any answer to get the right regex to be used in Java for a filter in edit text.我找不到任何答案来获得正确的正则表达式以在 Java 中用于编辑文本中的过滤器。 Must specify that I am not allowed to change project setting therefore some errors pop-up such as:必须指定我不允许更改项目设置,因此会弹出一些错误,例如:

'\s' and '\' escape sequences are not supported at language level '8'
Illegal escape character in string literal

Here are a few variants that I tried:以下是我尝试的一些变体:

"^[^ ]+.+"
"^[a-zA-Z!@#$%^&*()_+-={};',.<>?/:][ ]*.*"
"^[a-zA-Z0-9_ ]*$"
"^[^ ]+( .+)*$"
"^\\w+( \\w+)*$"
"^- (\\w+( \\w+)*)$"
"^[a-zA-Z0-9_][a-zA-Z0-9_ ]*[a-zA-Z0-9_]$"
"^[^ ]+[\\s]*"
"^[^-\\s][\w\s-]+$"

None of them works它们都不起作用

EDIT编辑

Here is all the code:这是所有代码:


        final EditText nameInput = line2.findViewById(R.id.input_value);
        nameInput.setFilters(new InputFilter[] {new NoSpaceStartInputFilter()});

private static class NoSpaceStartInputFilter implements InputFilter {
        
        private Pattern mPattern;
        
        public NoSpaceStartInputFilter() {
            mPattern = Pattern.compile("^[^ ]+.+");
            mPattern = Pattern.compile("^[a-zA-Z!@#$%^&*()_+-={};',.<>?/:][ ]*.*");
            mPattern = Pattern.compile("^[a-zA-Z0-9_ ]*$");
            mPattern = Pattern.compile("^[^ ]+( .+)*$");
            mPattern = Pattern.compile("^\\w+( \\w+)*$");
            mPattern = Pattern.compile("^- (\\w+( \\w+)*)$");
            mPattern = Pattern.compile("^[a-zA-Z0-9_][a-zA-Z0-9_ ]*[a-zA-Z0-9_]$");
            mPattern = Pattern.compile("^[^ ]+[\\s]*");
            mPattern = Pattern.compile("^[^-\\s][\w\s-]+$");
            mPattern = Pattern.compile("^(?!\\s+\\S)\\S*\\s.*");
        }

        @Override
        public CharSequence filter(CharSequence charSequence, int i, int i1, Spanned spanned, int i2, int i3) {
            Matcher matcher = mPattern.matcher(charSequence);
            if (!matcher.matches())
                return "";
            return null;
        }
    }

You can use您可以使用

text.matches("\\S.*")

The String#matches requires a full string match and the pattern matches a string that matches String#matches需要完整的字符串匹配,并且模式匹配匹配的字符串

  • \S - a non-whitespace as the first char and then \S - 一个非空格作为第一个字符,然后
  • .* - any zero or more chars other than line break chars as many as possible. .* - 尽可能多的除换行符以外的任何零个或多个字符。

To allow line break chars, use要允许换行符,请使用

text.matches("(?s)\\S.*")
text.matches("\\S[\\w\\W]*")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM