2

那么,下一个正则表达式实际上是什么意思呢?

str_([^_]*)(_[_0-9a-z]*)?

它是否等于^str_[^_]*(_[_0-9a-z]*)?

4

3 回答 3

5

str_从字面上匹配这些字符

([^_]*)匹配任何非下划线的字符 0 次或更多次

(_[_0-9a-z]*)?匹配另一个下划线,然后匹配 0 个或多个字符_0-9a-z。最后一部分是可选的。

我最近写了一篇非常简短的正则表达式介绍,希望对您有所帮助。.

于 2012-05-10T12:06:08.803 回答
4

As mentioned in the comments to my answer, http://gskinner.com/RegExr/ explains everything about a regular expression if you put it in.

str_([^_]*)(_[_0-9a-z]*)?
\  /^\  /^^^^\       /^^^
 \/ | \/ |||| \     / |||
 |  | |  ||||  \   /  ||`- Previous group is optional
 |  | |  ||||   \ /   |`-- End second capture group (an underscore and any amount of characters _, 0-9 or a-z)
 |  | |  ||||    |    `--- Match any amount (0-infinite) of previous (any character _, 0-9 or a-z)
 |  | |  ||||    `-------- Match any of the characters inside brackets
 |  | |  ||||              (0-9 means any number between 0 and 9, a-z means any lower case char between a and z)
 |  | |  |||`------------- Match "_" literally
 |  | |  ||`-------------- Start second capture group
 |  | |  |`--------------- End first capture group (any amount of any character which is not underscore)
 |  | |  `---------------- Match any amount (0-infinite) of previous (any character which is not underscore)
 |  | `------------------- ^ at the start inside [] means match any character not after ^
 |  |                      (In this case, match any which is not underscore)
 |  `--------------------- Start first capture group
 `------------------------ Match "str_" literally

The ^ at the start of ^str_[^_]*(_[_0-9a-z]*)? just means that it should only match at the start of line of anything you input.

于 2012-05-10T12:36:49.163 回答
0

str_([^_]*)(_[_0-9a-z]*)?

所以,让我们谈谈正则表达式:

1) ([^_]*)- 捕获()任何计数(可能为零)*符号[],不是^这个符号_

2) (_[_0-9a-z]*)?- 存在或不存在?并捕获()序列的开头符号_和序列的尾部,其中出现在任何计数中的*任何符号,如集合[]中的_ a,b,c,..,z0,1,..9

于 2012-05-10T12:11:26.607 回答