简体   繁体   English

此正则表达式模式如何工作

[英]How does this regex pattern work

I am learning regex and I am currently looking at this page http://regexone.com/example/0 I am following everything ok so far but I dont understand how this one works 我正在学习正则表达式,目前正在查看此页面http://regexone.com/example/0到目前为止,我一直在进行一切正常的操作,但我不了解此工作原理

^-?\d+(,\d+)*(\.\d+(e\d+)?)?$

It matches the following text: 它与以下文本匹配:

  • 3.14529 3.14529
  • -255.34 -255.34
  • 128 128
  • 1.9e10 1.9e10
  • 123,34.00 123,34.00

And skips this one: 并跳过这一步:

  • 720p 720P

The solution explains it like this 解决方案这样解释

For the above example, the expression '^-?\\d+(,\\d+)*(.\\d+(e\\d+)?)?$' will match a string that starts with an optional negative sign, one or more digits, optionally followed by a comma and more digits, followed by an optional fractional component which consists of a period, one or more digits, and another optional component, the exponent followed by more digits. 对于上面的示例,表达式'^-?\\ d +(,\\ d +)*(。\\ d +(e \\ d +)?)?$'将匹配以可选负号,一个或多个数字开头的字符串, (可选)后跟一个逗号和多个数字,然后是一个可选的小数部分,该部分由一个句点,一个或多个数字以及另一个可选的部分组成,指数后面是更多的数字。

The * is where I get confused. *是让我感到困惑的地方。 I read it like this: 我这样阅读:

^                 Start
-?                Optional Negative
\d+               One or more digits
(,\d+)*           Group-comma and one or more digits - the * confuses me here
(\.\d+(e\d+)?)?   Optional group of full stop, one or more digits, another optional group of e and 1 or more digits

As I said the * confuses me. 正如我所说的,*使我感到困惑。 I think it is something to do with Variable content but I dont understand how it works. 我认为这与“可变内容”有关,但我不了解它的工作原理。

The * is simply a quantifier that states: 'match 0 or more of the previous match', in this case (,\\d+) . *只是表示以下内容的量词:“匹配上一个匹配项0个或多个”,在这种情况下(,\\d+)

In this case, it would match something like: 在这种情况下,它将匹配以下内容:

,123,123,123,63,345,345,346,3245235,234  (of any length but similar pattern)

正则表达式可视化

Debuggex Demo Debuggex演示

that means that zero or more times a group like this ,34 is allowed. 这意味着zeromore次这样的组,34是允许的。

group like ,34 or ,666 is represented as (,\\d+)* ,34,666类的组表示为(,\\d+)*

so that it allows text 122,222,12 or 323,212,22 因此它允许文本122,222,12或323,212,22

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM