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

发布于 2024-10-05 23:33:30 字数 141 浏览 0 评论 0原文

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

提前致谢, 蒂姆

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?

Thanks in advance,
Tim

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

汹涌人海 2024-10-12 23:33:31

试试这个:

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

更新:

现在我看到问题已经更新为引用 MySQL,这显着改变了事情。上述正则表达式使用了 MySQL 不支持的非捕获括号。但这也引出了一个问题:你真的应该使用正则表达式来解决这个问题吗?我们确实必须看看您如何存储必须在 1 到 50 之间的数字。它们是 varchar 吗?它们是 int 吗?我将演示如何以两种方式解决这个问题。首先,我将设置一个带有索引的测试表:

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;

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

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

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

mysql> select * from regextest where varchar_number rlike '^([1-9]|[1-4][0-9]|50)

这可行,但在大型数据集上表现不佳,因为即使存在索引,它也无法使用索引。 MySQL 必须对表中的每一行运行一次正则表达式。假设 1 到 50 之间的数字以 int 形式存储在 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)

该查询将执行良好,因为它可以使用索引,并且它也更具可读性和更易于维护。全面获胜。

; +----+----------------+------------+ | id | varchar_number | int_number | +----+----------------+------------+ | 2 | 1 | 1 | | 3 | 35 | 35 | | 4 | 49 | 49 | | 5 | 50 | 50 | +----+----------------+------------+ 4 rows in set (0.00 sec)

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

该查询将执行良好,因为它可以使用索引,并且它也更具可读性和更易于维护。全面获胜。

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:

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:

mysql> select * from regextest where varchar_number rlike '^([1-9]|[1-4][0-9]|50)

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> 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.

; +----+----------------+------------+ | 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:

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

洒一地阳光 2024-10-12 23:33:31
'^(0?\d|[1-4]\d|50)

即:

  • 输入的开头
  • 后跟一个数字(可选前跟 0),或者
  • 后跟 1-4 后跟任意数字,或者
  • 后跟 50
  • ,然后确保我们看到输入的结尾。

编辑:上面允许 0(和 00),但您肯定不想要。因此,假设您实际上并不希望允许使用前导零:

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

编辑: 由于 OP 的后续评论表明这是针对 MySQL 的,因此我更改了指定模式的语法。

即:

  • 输入的开头
  • 后跟一个数字(可选前跟 0),或者
  • 后跟 1-4 后跟任意数字,或者
  • 后跟 50
  • ,然后确保我们看到输入的结尾。

编辑:上面允许 0(和 00),但您肯定不想要。因此,假设您实际上并不希望允许使用前导零:


编辑: 由于 OP 的后续评论表明这是针对 MySQL 的,因此我更改了指定模式的语法。

编辑: 由于 OP 的后续评论表明这是针对 MySQL 的,因此我更改了指定模式的语法。

即:

  • 输入的开头
  • 后跟一个数字(可选前跟 0),或者
  • 后跟 1-4 后跟任意数字,或者
  • 后跟 50
  • ,然后确保我们看到输入的结尾。

编辑:上面允许 0(和 00),但您肯定不想要。因此,假设您实际上并不希望允许使用前导零:

编辑: 由于 OP 的后续评论表明这是针对 MySQL 的,因此我更改了指定模式的语法。

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

That is:

  • The start of the input
  • followed by a single digit (optional preceded by a 0), or
  • followed by a 1-4 followed by any digit, or
  • followed by 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:

'^([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.

That is:

  • The start of the input
  • followed by a single digit (optional preceded by a 0), or
  • followed by a 1-4 followed by any digit, or
  • followed by 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:


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

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

That is:

  • The start of the input
  • followed by a single digit (optional preceded by a 0), or
  • followed by a 1-4 followed by any digit, or
  • followed by 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:

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

农村范ル 2024-10-12 23:33:31

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

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

青巷忧颜 2024-10-12 23:33:31

尝试这个

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

将执行以下操作

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

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
ぇ气 2024-10-12 23:33:31

使用 Perl6::Rules 进行“作弊”:

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

或者在 Perl 5.10 及更高版本中,

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

"Cheating" with Perl6::Rules:

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

Or in Perl 5.10 and up,

/(\d+)(?(?{1>$1||50<$1})(*FAIL))/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文