【问题标题】:regular expression not work in android正则表达式在android中不起作用
【发布时间】:2017-07-04 09:58:06
【问题描述】:

嗨,我想逐个字符地匹配名字,但这会出错,这是我的代码:

int length = input.length();
for (int i = 0; i < length; i++){
    char ch = input.charAt(i);
    String regex ="^[_\\s]||[آ-ی]$";
    Matcher matcher = Pattern.compile( regex ).matcher(ch);

这是我的完整代码:

public class MainActivity extends AppCompatActivity {
    EditText editText;
    Button button;
    String input;
    String result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = (EditText)findViewById(R.id.edt);
        button = (Button)findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("ShowToast")
            @Override
            public void onClick(View v) {
               input = editText.getText().toString();
                check_reg();
            }
        });

    }
    public  boolean check_reg(){
        int length = input.length();
        for (int i = 0; i < length; i++){
            char ch = input.charAt(i);
            String regex ="^[_\\s]||[آ-ی]$";
            Matcher matcher = Pattern.compile( regex ).matcher(ch);
            if (matcher.find( ))
            {
                result = matcher.group();
                Toast.makeText(MainActivity.this, "match", Toast.LENGTH_SHORT).show();
            }
            else
            {
                Toast.makeText(MainActivity.this, "no match", Toast.LENGTH_SHORT).show();
            }

        }
        return false;
    }
}

这是我的问题的图像:

【问题讨论】:

  • 我猜是java的东西?
  • 从我在docs 中读到的内容来看,pattern.matcher() 方法只接受StringCharSequence 类型...不接受char
  • 为什么要逐个字符检查?您可以使用^[_\\sآ-ی]+$ 来检查它作为一个完整的字符串。 input.matches("[_\\sآ-ی]+")
  • @nazanin 询问 Jawad Ysari

标签: java regex validation android-edittext


【解决方案1】:

问题是您将char 类型传递给&lt;Pattern&gt;.matcher() 方法,该方法只接受StringCharSequence 类型。 Here are the docs that explain it.

char ch = input.charAt(i);
String regex ="^[_\\s]||[آ-ی]$";
Matcher matcher = Pattern.compile( regex ).matcher(ch);

修复错误所需要做的就是在将char ch 变量传递给matcher() 方法时将其转换为字符串。

char ch = input.charAt(i);
String regex ="^[_\\s]||[آ-ی]$";
Matcher matcher = Pattern.compile(regex).matcher(Character.toString(ch));

【讨论】:

    【解决方案2】:

    编译器告诉您必须将String 传递给方法matcher()。您不能将char 传递给它。

    如果你想要的话,你可以创建一个长度为 1 的字符串。

    你可以用更自然的方式使用正则表达式,让它为你做匹配。例如:

    public  boolean check_reg(){
        String regex ="^(?:[_\\s]||[آ-ی])+$";
        Matcher matcher = Pattern.compile( regex ).matcher(input);
        if (matcher.find( ))
            {
                result = matcher.group();
                Toast.makeText(MainActivity.this, "match", Toast.LENGTH_SHORT).show();
                return true;
            }
            else
            {
                Toast.makeText(MainActivity.this, "no match", Toast.LENGTH_SHORT).show();
                return false;
            }
    }
    

    此模式将逐个字符匹配整个输入字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-08
      • 1970-01-01
      • 1970-01-01
      • 2012-12-18
      • 2014-09-12
      相关资源
      最近更新 更多