如何使用正则表达式来表示字母数字表达式 [英] How to use regex for alphanumeric expressions

查看:158
本文介绍了如何使用正则表达式来表示字母数字表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在文本框中使用字母数字值。该框必须包含4个字符。不多或少。虽然我下面的代码适用于字母数字。我无法识别4个字符。

你能帮帮忙吗?



我尝试了什么:



I'm trying to us Alphanumeric Values in a textbox. The box must contain 4 characters. No more or less. While my code below will work for Alphanumeric. I can't get it to recognize 4 characters.
Can you help?

What I have tried:

public void AlphaNumericString()
       {

           //string value;
           string s = BacBox.Text.ToString();
           int.TryParse(s, out int result);


           Regex r = new Regex("^[a-zA-Z0-9]*$");
           if (r.IsMatch(s) && (s.Length > 3  && s.Length< 5 ))
           {

                   BacBox.Text = s;

           }
          else
           {
               MessageBox.Show("Enter 4 alphanumeric characters only");
           }


       }

推荐答案

);
if(r.IsMatch(s)&&(s.Length> 3& s.Length< 5))
{

BacBox.Text = s;

}
其他
{
MessageBox.Show(仅输入4个字母数字字符);
}


}
"); if (r.IsMatch(s) && (s.Length > 3 && s.Length< 5 )) { BacBox.Text = s; } else { MessageBox.Show("Enter 4 alphanumeric characters only"); } }


尝试:

Try:
^[a-zA-Z0-9]{4}




获取 Expresso [ ^ ] - 它是免费的,它会检查并生成正则表达式。


Get a copy of Expresso[^] - it's free, and it examines and generates Regular expressions.


这篇关于如何使用正则表达式来表示字母数字表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆