空字符串的正则表达式或10位数

时间:2022-12-22 19:24:53

To start: I've just started with Regexes, and I've had a really difficult time understanding what's going on with them. Just a heads-up.

首先:我刚开始使用Regexes,而且我很难理解他们正在发生什么。只是一个单挑。

For a project, I'm trying to develop a regex that takes either A) an empty string (in C# speak, ""), or B) ten (10) digits. I've been able to figure out how to accomplish the 10-digits part:

对于一个项目,我正在尝试开发一个正则表达式,它采用A)空字符串(在C#speak,“”)或B)十(10)位数。我已经弄清楚如何完成10位数的部分:

"^[0-9X]{10}$"

...But not the 'empty string or' part. which I imagine would be something like:

......但不是'空串或'部分。我想这将是这样的:

"^[]$|^[0-9X]{10}$"

Obviously that doesn't work, but I have no idea how to write something that does, even though there are quite a few topics on the matter.

显然这不起作用,但我不知道如何写一些有用的东西,即使有很多关于这个问题的话题。

Questions:

问题:

A) What is a regex that will return true if the given string is either a string.Empty (rather, ""), or exactly 10 digits?

A)如果给定的字符串是string.Empty(而不是“”),或者恰好是10位数,那么正则表达式将返回true?

B) Please explain how exactly it works. It's not that I haven't been trying to learn (I did figure out that the ^$ are anchors for exact-string matching, and that | is the OR operator...), it's just that regexes apparently do not come naturally to me...yet, I'm in a situation I must use them.

B)请解释它究竟是如何工作的。这不是我没有尝试学习(我确实发现^ $是精确字符串匹配的锚点,而且|是OR运算符...),这只是正则表达式显然不是自然而然的我......但是,我处在一种我必须使用它们的情况。

4 个解决方案

#1


18  

(^$)|(^\d{10}$)

The first option matches the empty string, the second option matches 10 digits.

第一个选项匹配空字符串,第二个选项匹配10个数字。

I don't know what your X is for, unless you're looking for a hex string, if that is the case, you'll want to do the following:

我不知道你的X是什么,除非你正在寻找一个十六进制字符串,如果是这样的话,你会想要做以下事情:

(^$)|(^[0-9a-fA-FxX]{10}$)

#2


3  

^$|^[0-9X]{10}$

^ means match the start, $ means match the end as there is nothing in between, match nothing. If there is something, this doesn't match

^表示匹配开头,$表示匹配结束,因为两者之间没有任何内容,什么都不匹配。如果有什么,这是不匹配的

| is the alternation operator, between alternatives

|是备选方案之间的交替运营商

#3


1  

 string a = "0123456789";
 string b = "";
 string reg = @"^(|\d{10})$";

 if ( Regex.IsMatch( a, reg ) && Regex.IsMatch( b, reg ) ) {
     Console.WriteLine( "Matched" );
 }

\d is equivalent to [0-9]
{10} is ten times exactly
| is the OR operator
^ is the start, $ is the end
The brackets limit the OR operation to nothing or ten digits between the start and end.

\ d相当于[0-9] {10}十倍于|是OR运算符^是开始,$是结束括号将OR运算限制为空或开始和结束之间的十位数。

#4


0  

Try the below REGEX ... it will work...

试试下面的REGEX ...它会起作用......

        if (Regex.IsMatch(textBox1.Text, @"^(?!\s*$).+")) //Check Not Empty String 
        {
            if (Regex.IsMatch(textBox1.Text, @"^\d{10}$")) // Check ten digits - Not allowed Alphanumeric
            {
                MessageBox.Show("find Ten digits");
            }
            else
            {
                    MessageBox.Show("Error");
            }
        }
        else
        {
            MessageBox.Show("Empty String Found");
        }

#1


18  

(^$)|(^\d{10}$)

The first option matches the empty string, the second option matches 10 digits.

第一个选项匹配空字符串,第二个选项匹配10个数字。

I don't know what your X is for, unless you're looking for a hex string, if that is the case, you'll want to do the following:

我不知道你的X是什么,除非你正在寻找一个十六进制字符串,如果是这样的话,你会想要做以下事情:

(^$)|(^[0-9a-fA-FxX]{10}$)

#2


3  

^$|^[0-9X]{10}$

^ means match the start, $ means match the end as there is nothing in between, match nothing. If there is something, this doesn't match

^表示匹配开头,$表示匹配结束,因为两者之间没有任何内容,什么都不匹配。如果有什么,这是不匹配的

| is the alternation operator, between alternatives

|是备选方案之间的交替运营商

#3


1  

 string a = "0123456789";
 string b = "";
 string reg = @"^(|\d{10})$";

 if ( Regex.IsMatch( a, reg ) && Regex.IsMatch( b, reg ) ) {
     Console.WriteLine( "Matched" );
 }

\d is equivalent to [0-9]
{10} is ten times exactly
| is the OR operator
^ is the start, $ is the end
The brackets limit the OR operation to nothing or ten digits between the start and end.

\ d相当于[0-9] {10}十倍于|是OR运算符^是开始,$是结束括号将OR运算限制为空或开始和结束之间的十位数。

#4


0  

Try the below REGEX ... it will work...

试试下面的REGEX ...它会起作用......

        if (Regex.IsMatch(textBox1.Text, @"^(?!\s*$).+")) //Check Not Empty String 
        {
            if (Regex.IsMatch(textBox1.Text, @"^\d{10}$")) // Check ten digits - Not allowed Alphanumeric
            {
                MessageBox.Show("find Ten digits");
            }
            else
            {
                    MessageBox.Show("Error");
            }
        }
        else
        {
            MessageBox.Show("Empty String Found");
        }