正则表达式为1-50,不带小数点

时间:2022-06-12 17:10:57

I am looking for a regular expression that will match any number from 1 to 50 inclusive. So far, I have found examples but they all allow the string to contain a decimal point, which I do not want to include. So 1,13,24,50 are OK but 1. ,etc are not. Is there a REGEXP that I can use?

我正在寻找一个正则表达式,它将匹配1到50之间的任何数字。到目前为止,我已经找到了示例,但它们都允许字符串包含小数点,我不想包含它。所以1,13,24,50可以,但1.等等。我可以使用REGEXP吗?

Thanks in advance, Tim

蒂姆,提前谢谢

5 个解决方案

#1


6  

Try this:

/^(?:[1-9]|[1-4][0-9]|50)$/

UPDATE:

Now that I see the question has been updated to refer to MySQL, this changes things significantly. The above-mentioned regular expression uses non-capturing parens which are not supported by MySQL. But it also begs the question; should you really be using regular expressions to solve this problem? We really have to look at how you are storing your numbers that must be between 1 and 50. Are they varchars? Are they ints? I'll demonstrate how to solve it both ways. First I'll set up a test table with indexes:

现在我看到问题已经更新以引用MySQL,这显着改变了一些事情。上述正则表达式使用MySQL不支持的非捕获parens。但它也引出了一个问题;你真的应该使用正则表达式来解决这个问题吗?我们真的要看看你如何存储必须在1到50之间的数字。它们是varchars吗?他们是彗星吗?我将演示如何以两种方式解决它。首先,我将建立一个带索引的测试表:

create table regextest (
    id int unsigned primary key auto_increment,
    varchar_number varchar(5) not null,
    int_number int not null,
    index(varchar_number),
    index(int_number)
) engine=innodb;

Now put some test data into it making sure all our edge cases are covered:

现在将一些测试数据放入其中,确保涵盖所有边缘情况:

insert into regextest (varchar_number, int_number)
    values ('0', 0), ('1', 1), ('35', 35), ('49', 49), ('50', 50), ('51', 51);

And now, here is a query that will solve your problem assuming that your numbers are stored as strings in the varchar_number column:

现在,这是一个解决您的问题的查询,假设您的数字在varchar_number列中存储为字符串:

mysql> select * from regextest where varchar_number rlike '^([1-9]|[1-4][0-9]|50)$';
+----+----------------+------------+
| id | varchar_number | int_number |
+----+----------------+------------+
|  2 | 1              |          1 |
|  3 | 35             |         35 |
|  4 | 49             |         49 |
|  5 | 50             |         50 |
+----+----------------+------------+
4 rows in set (0.00 sec)

This works but it will perform poorly on large data sets because it can't use an index even if one is present. MySQL must run the regular expression once for every row in the table. Suppose your numbers between 1 and 50 were stored as ints in the int_number column. You could simply do this:

这可以工作,但它在大型数据集上表现不佳,因为即使存在一个索引也无法使用索引。 MySQL必须为表中的每一行运行一次正则表达式。假设1到50之间的数字作为整数存储在int_number列中。你可以这样做:

mysql> select * from regextest where int_number between 1 and 50;
+----+----------------+------------+
| id | varchar_number | int_number |
+----+----------------+------------+
|  2 | 1              |          1 |
|  3 | 35             |         35 |
|  4 | 49             |         49 |
|  5 | 50             |         50 |
+----+----------------+------------+
4 rows in set (0.00 sec)

This query will perform well because it can use an index and it's also more readable and more maintainable. Wins all around.

此查询将表现良好,因为它可以使用索引,它也更易读,更易于维护。赢得四周。

#2


4  

'^(0?\d|[1-4]\d|50)$'

That is:

  • The start of the input
  • 输入的开始

  • followed by a single digit (optional preceded by a 0), or
  • 后跟一个数字(可选,前面加一个0),或

  • followed by a 1-4 followed by any digit, or
  • 然后是1-4后跟任何数字,或

  • followed by 50
  • 接着是50

  • and then ensure that we see the end of the input.
  • 然后确保我们看到输入的结束。

Edit: The above allows 0 (and 00) which you surely don't want. So, assuming you didn't really want leading zeros allowed anyhow:

编辑:上面允许0(和00)你肯定不想要。所以,假设你真的不希望无论如何允许前导零:

'^([1-9]|[1-4]\d|50)$'

Edit: As the OP's later comments indicate that this is for MySQL, I've changed the syntax for specifying the pattern.

编辑:由于OP后来的评论表明这是针对MySQL的,我已经改变了指定模式的语法。

#3


1  

^([1-9]|[1-4][0-9]|50)$

#4


0  

try this one

试试这个

([0-4]{1}[0-9]{0,1}[.]{0,1}[0-9]{0,2}|50)

will Do Following

将遵循

45.00 - Match
4     - Match
78    - Not Match
51    - Not Match
21.25 - Match

#5


0  

"Cheating" with Perl6::Rules:

使用Perl6“欺骗”::规则:

/ ( \d+ ) { 1 <= $1 && $1 <= 50 or fail } /

Or in Perl 5.10 and up,

或者在Perl 5.10以及

/(\d+)(?(?{1>$1||50<$1})(*FAIL))/

#1


6  

Try this:

/^(?:[1-9]|[1-4][0-9]|50)$/

UPDATE:

Now that I see the question has been updated to refer to MySQL, this changes things significantly. The above-mentioned regular expression uses non-capturing parens which are not supported by MySQL. But it also begs the question; should you really be using regular expressions to solve this problem? We really have to look at how you are storing your numbers that must be between 1 and 50. Are they varchars? Are they ints? I'll demonstrate how to solve it both ways. First I'll set up a test table with indexes:

现在我看到问题已经更新以引用MySQL,这显着改变了一些事情。上述正则表达式使用MySQL不支持的非捕获parens。但它也引出了一个问题;你真的应该使用正则表达式来解决这个问题吗?我们真的要看看你如何存储必须在1到50之间的数字。它们是varchars吗?他们是彗星吗?我将演示如何以两种方式解决它。首先,我将建立一个带索引的测试表:

create table regextest (
    id int unsigned primary key auto_increment,
    varchar_number varchar(5) not null,
    int_number int not null,
    index(varchar_number),
    index(int_number)
) engine=innodb;

Now put some test data into it making sure all our edge cases are covered:

现在将一些测试数据放入其中,确保涵盖所有边缘情况:

insert into regextest (varchar_number, int_number)
    values ('0', 0), ('1', 1), ('35', 35), ('49', 49), ('50', 50), ('51', 51);

And now, here is a query that will solve your problem assuming that your numbers are stored as strings in the varchar_number column:

现在,这是一个解决您的问题的查询,假设您的数字在varchar_number列中存储为字符串:

mysql> select * from regextest where varchar_number rlike '^([1-9]|[1-4][0-9]|50)$';
+----+----------------+------------+
| id | varchar_number | int_number |
+----+----------------+------------+
|  2 | 1              |          1 |
|  3 | 35             |         35 |
|  4 | 49             |         49 |
|  5 | 50             |         50 |
+----+----------------+------------+
4 rows in set (0.00 sec)

This works but it will perform poorly on large data sets because it can't use an index even if one is present. MySQL must run the regular expression once for every row in the table. Suppose your numbers between 1 and 50 were stored as ints in the int_number column. You could simply do this:

这可以工作,但它在大型数据集上表现不佳,因为即使存在一个索引也无法使用索引。 MySQL必须为表中的每一行运行一次正则表达式。假设1到50之间的数字作为整数存储在int_number列中。你可以这样做:

mysql> select * from regextest where int_number between 1 and 50;
+----+----------------+------------+
| id | varchar_number | int_number |
+----+----------------+------------+
|  2 | 1              |          1 |
|  3 | 35             |         35 |
|  4 | 49             |         49 |
|  5 | 50             |         50 |
+----+----------------+------------+
4 rows in set (0.00 sec)

This query will perform well because it can use an index and it's also more readable and more maintainable. Wins all around.

此查询将表现良好,因为它可以使用索引,它也更易读,更易于维护。赢得四周。

#2


4  

'^(0?\d|[1-4]\d|50)$'

That is:

  • The start of the input
  • 输入的开始

  • followed by a single digit (optional preceded by a 0), or
  • 后跟一个数字(可选,前面加一个0),或

  • followed by a 1-4 followed by any digit, or
  • 然后是1-4后跟任何数字,或

  • followed by 50
  • 接着是50

  • and then ensure that we see the end of the input.
  • 然后确保我们看到输入的结束。

Edit: The above allows 0 (and 00) which you surely don't want. So, assuming you didn't really want leading zeros allowed anyhow:

编辑:上面允许0(和00)你肯定不想要。所以,假设你真的不希望无论如何允许前导零:

'^([1-9]|[1-4]\d|50)$'

Edit: As the OP's later comments indicate that this is for MySQL, I've changed the syntax for specifying the pattern.

编辑:由于OP后来的评论表明这是针对MySQL的,我已经改变了指定模式的语法。

#3


1  

^([1-9]|[1-4][0-9]|50)$

#4


0  

try this one

试试这个

([0-4]{1}[0-9]{0,1}[.]{0,1}[0-9]{0,2}|50)

will Do Following

将遵循

45.00 - Match
4     - Match
78    - Not Match
51    - Not Match
21.25 - Match

#5


0  

"Cheating" with Perl6::Rules:

使用Perl6“欺骗”::规则:

/ ( \d+ ) { 1 <= $1 && $1 <= 50 or fail } /

Or in Perl 5.10 and up,

或者在Perl 5.10以及

/(\d+)(?(?{1>$1||50<$1})(*FAIL))/