我有这个正则表达式来匹配我的系统中的用户名:/[a-zA-Z]+([_-]?[0-9a-zA-Z]+)*$/
。它以字母开头,允许numbers, _ and -
。但我不希望用户名包含admin
,facebook
,official
等字词。如何将这些字词添加到正则表达式?我想使用一个正则表达式来管理所有约束。
答案 0 :(得分:1)
这是一种可能性:
^((?!admin|facebook|official)[a-zA-Z])((?!admin|facebook|official|[-_]{2})[-_0-9a-zA-Z])*$
修改:已更新,以提高效率。
答案 1 :(得分:1)
public int GenerateRandomNo()
{
int _min = 0000;
int _max = 9999;
Random _rdm = new Random();
return _rdm.Next(_min, _max);
}
public int rand_num()
{
randnum = GenerateRandomNo();
string createText = randnum.ToString() + Environment.NewLine;
string file_path = System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + @"\Invoices\numbers.txt";
File.AppendAllText(file_path, createText);
int number = File.ReadLines(file_path).Count(); //count number of lines in file
System.IO.StreamReader file = new System.IO.StreamReader(file_path);
do
{
randnum = GenerateRandomNo();
}
while ((file.ReadLine()) == randnum.ToString());
file.Close();
return randnum;
}
/^(?=((?!(admin|facebook|official)).)+$)[a-z]+[_-]?[0-9a-z]+$/i

input { width: 100%; box-sizing: border-box; outline: none; }
:valid { border: 1px solid green; }
:invalid { border: 1px solid red; }