The regular expression ^[A-Za-z](\W|\w)*
matches when the user gives the first letter as white space, and the first letter should not be a digit and remaining letters may be alpha numerical. When the user gives a white space as the first character it should automatically be trimmed. How?
正则表达式^[A-Za-z](\ W | \ W)*匹配当用户给第一个字母为空白,不应该第一个字母一个数字和剩下的字母可能是α数值。当用户将空格作为第一个字符时,它应该自动被修改。如何?
2 个解决方案
#1
2
^\s*([A-Za-z]\w*)
Should do it. Just get group 1.
应该这样做。组1。
I'm not sure the language you are using, I'm going to assume C#, so here is a C# sample:
我不确定你用的是哪种语言,我假设是c#,这里有一个c#示例:
string testString = " myMatch123 not in the match";
Regex regexObj = new Regex("^\\s*([A-Za-z]\\w*)",
RegexOptions.IgnoreCase | RegexOptions.Multiline);
string result = regexObj.Match(testString).Groups[1].Value;
Console.WriteLine("-" + result + "-");
This will print
这将打印
-myMatch123-
to the console window.
到控制台窗口。
#2
1
Is it possible to Trim()
your input before giving it to your regex?
是否可以在将输入内容提供给regex之前对其进行修剪?
If you're looking for alpha-numerical, starting with non-numeric, you probably want:
如果您正在寻找alpha- numbers,从非numeric开始,您可能需要:
\s*([A-Za-z][A-Za-z0-9]+)
If you allow one-character user names, change that plus to a star.
如果你只允许一个字符的用户名,那就把这个加号改成星号。
#1
2
^\s*([A-Za-z]\w*)
Should do it. Just get group 1.
应该这样做。组1。
I'm not sure the language you are using, I'm going to assume C#, so here is a C# sample:
我不确定你用的是哪种语言,我假设是c#,这里有一个c#示例:
string testString = " myMatch123 not in the match";
Regex regexObj = new Regex("^\\s*([A-Za-z]\\w*)",
RegexOptions.IgnoreCase | RegexOptions.Multiline);
string result = regexObj.Match(testString).Groups[1].Value;
Console.WriteLine("-" + result + "-");
This will print
这将打印
-myMatch123-
to the console window.
到控制台窗口。
#2
1
Is it possible to Trim()
your input before giving it to your regex?
是否可以在将输入内容提供给regex之前对其进行修剪?
If you're looking for alpha-numerical, starting with non-numeric, you probably want:
如果您正在寻找alpha- numbers,从非numeric开始,您可能需要:
\s*([A-Za-z][A-Za-z0-9]+)
If you allow one-character user names, change that plus to a star.
如果你只允许一个字符的用户名,那就把这个加号改成星号。