【问题标题】:Validate [Preference] filename from EditText using regex使用正则表达式从 EditText 验证 [Preference] 文件名
【发布时间】:2014-10-17 22:37:21
【问题描述】:

[第一个应用程序] 我为用户提供了使用不同首选项文件拥有多个配置文件的选项。用户可以自己命名他们的个人资料。我制作了一个自定义对话框以将文件名作为编辑文本中的输入。一个有效的文件名应该只包含 [A-Z, a-z, 0-9, _, -]。我让用户知道这个限制。

每当用户输入无效字符时,我希望从 edittext 中删除该字符,并最好显示一个 toast。经过简短的搜索后,我相信可以使用正则表达式轻松执行此任务(与我在 edittext 上使用 textwatcher 的基于字符串的天真方法相比)。但是,我以前从未使用过正则表达式,因此需要有关此方面的帮助。

ps - 一个令人担忧的用例是用户发狂并在编辑文本中键入随机字符。

【问题讨论】:

    标签: java android regex android-edittext


    【解决方案1】:

    这是上述特定场景的工作代码,我们将文本观察器附加到我们的编辑文本,然后使用模式验证输入(Thnaks to huidube):

        profile_name.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) { }
    
            @Override
            public void afterTextChanged(Editable s) {
                String str = s.toString();
                if (!(str.matches("[a-zA-Z0-9-_ ]*"))) {
                    str = removeIllegalChar(str).trim(); //trim whitespaces
                    profile_name.setText(str);
                    profile_name.setSelection(str.length());  //use only if u want to set cursor to end
                }
            }
        });
    
        private String removeIllegalChar(String str) {
        for (int i=0; i < str.length(); i++) {
            if (!(String.valueOf(str.charAt(i)).matches("[a-zA-Z0-9-_ ]*"))) {
                //as the callback is called for each character entered, we can return on first non-match
                //maybe show a short toast        
                return str.substring(0, i) + str.substring(i+1);
            }
        }
        return str;
    }
    

    我有两台测试设备,一台是垂死的老兵,一台是 Moto G,上面的程序在两者上都能顺利运行。所以,我想这是可以接受的最佳选择。

    【讨论】:

      【解决方案2】:

      每次输入完成时,我都会检查每个字母是否匹配您的正则表达式。

      我正在使用http://regexpal.com/

      你需要类似的东西:

      public String validateInput(String input){
          String input = "yourStr1ng$";
          for (int i = 0; i < input.length(); i++) {
              if ((input .charAt(i) + "").matches("[1-9]||[a-z]")) {
                  System.out.println(input.charAt(i) + " matching");
              } else {
                  System.out.println(input.charAt(i) + " failed");
                  //removeLetter(input charAt(i));
              }
          }
          return true;
      }
      

      返回:

      y matching
      o matching
      u matching
      r matching
      S failed
      t matching
      r matching
      1 matching
      n matching
      g matching
      $ failed
      

      【讨论】:

      • 抱歉回复晚了(印度的节日)。感谢您的提醒。使用正则表达式我登陆了一个正则表达式。因为,我需要实时验证edittext输入并更新它。我将编写一个工作代码并对其进行测试以获得最佳性能。如果一切正常,我会在这里发布代码。谢谢!
      猜你喜欢
      • 1970-01-01
      • 2015-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多