简体   繁体   English

正则表达式与数字和特殊字符,但没有字母

[英]Regex with numbers and special characters but no letters

I'm making a regex that accepts an input with any decimal(0-9), +, * or # but shouldn't accept any letters(az). 我正在制作一个接受带有任何小数(0-9),+,*或#的输入的正则表达式,但不应接受任何字母(az)。

so numbers like 所以数字就像

  • #192# #192#
  • *31#+32475728966 * 31#+ 32475728966
  • 0479266315 0479266315
  • +32495959511 32495959511

should be accepted. 应该被接受。

The regex is invalid when there is any letter in the string. 当字符串中有任何字母时,正则表达式无效。

  • #192#abbef #192#abbef
  • a0479266315 a0479266315

This is the regex I have so far: 这是我到目前为止的正则表达式:

private const string PhoneNumberRegex = "((\\d)|(\\*)|(\\#)|(\\+))?";

private bool IsValid(inputString)
{
    // Accept * # + and number
    Match match = Regex.Match(inputString, PhoneNumberRegex, RegexOptions.IgnoreCase);
    return match.Success;
}

But this regex also returns true on #192#abbef 但是这个正则表达式在#192#abbef上也会返回true

How can I fix this? 我怎样才能解决这个问题?

You can use this: 你可以用这个:

private const string PhoneNumberRegex = @"^[0-9*#+]+$";

Where ^ and $ are anchors for start and end of the string. 其中^$是字符串开头和结尾的锚点。

Note: RegexOptions.IgnoreCase is not needed. 注意: RegexOptions.IgnoreCase

You need to anchor your string with ^ (begin of line) and $ (end of line). 你需要用^ (行首)和$ (行尾)锚定你的字符串。 Otherwise your string matches if any part of it matches. 否则,如果匹配的任何部分匹配,则字符串匹配。

Also, you want at least one of those characters (you don't want to match an empty string, so you should use + (one or more). 此外,您至少需要其中一个字符(您不希望匹配空字符串,因此您应该使用+ (一个或多个)。

Lastly, you can make a character class out of all your characters: [\\d*#+] . 最后,你可以用你的所有角色制作一个角色类: [\\d*#+]

Putting it all together gives you: 把它们放在一起就可以了:

private const string PhoneNumberRegex = "^[\\d*#+]+$";

This means that from the beginning of the string till the end, you want one or more of those characters you listed. 这意味着从字符串的开头到结尾,您需要列出的一个或多个字符。

我相信这将满足您的需求

private const string PhoneNumberRegex = @"^([0-9]|#|\+|\*)+$"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 特定数字和特殊字符的正则表达式 - Regex for specific numbers and special characters 用于捕获字母之间具有特殊字符的单词的正则表达式 - Regex for catching word with special characters between letters 英文字母和特殊字符的正则表达式 - Regex Expression for english letters and special characters 检查正则表达式是否包含字母数字和下划线字符 - Check regex for letters numbers and underscore characters 数字的正则表达式,一些特殊字符和NULL - Regex for Numbers, some special characters and NULL 正则表达式仅检测特殊字符之间的数字 - Regex to detect numbers only between special characters 一开始它至少接受 3 个字母,然后在 .net 中接受数字和特殊字符 - the beginning it accepts at least 3 letters and from there numbers and special characters in .net C#在正则表达式中匹配字母,数字和特殊字符 - C# Matching letters, numbers and special character in regex 我需要验证字符串的第一个字符是否为字母,下一个字符是否为字母或数字或特殊字符 _、-、。 和空间 - I need to validate if first character of a string is a letter and next characters are either letters or numbers or special characters _,-,. and space 文本框的正则表达式只接受数字,逗号而不是0,十进制或特殊字符 - Regex for textbox to accept only numbers, comma and not 0, decimal or special characters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM