数字和任何长度的正则表达式

ser*_*nni 1 java regex

我知道String的确切前缀.

例如它是'XXX000'

在前缀,数字和字符数量为60之后.

如何构建regexp这种情况?

在我最初的理解中它应该看起来像:

(XXX000)(\w{*})
Run Code Online (Sandbox Code Playgroud)

喜欢:前缀(一些数字或一些字符)

谢谢.

Col*_*ert 5

用这个 /XXX000(\w{0,60})/

/    <- Start of the regex
    XXX000    <- Your constant prefix (don't really need to capture it then)
    (         <- Capture the following matching elements
        \w        <- Every characters in [a-zA-Z0-9_]
        {0,60}    <- Between 0 and 60 repetitions of the last element

    )         <- End of the group
/    <- End of the regex
Run Code Online (Sandbox Code Playgroud)

如果您不想要[a-zA-Z0-9_]字符,请使用您自己的字符类替换.

注意:您可能不需要分隔符,如果是这种情况,请记得删除分隔符.


资源:

  • @sergio如果你不关心(或不知道)数量,你可以使用`*`(零或更多)或`+`(一个或多个)重复运算符而不是`{n,m} ` (2认同)