这个正则表达式意味着什么?

时间:2012-05-10 12:00:05

标签: regex

那么,下一个regexp究竟意味着什么?

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

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

3 个答案:

答案 0 :(得分:5)

str_字面上匹配这些字符

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

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

I wrote recently a really brief regex introduction, hope it will help you.

答案 1 :(得分:4)

正如我在回答的评论中所提到的,http://gskinner.com/RegExr/解释了正则表达式的所有内容。

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

^开头的^str_[^_]*(_[_0-9a-z]*)?只意味着它只应在您输入的任何内容的开头处匹配。

答案 2 :(得分:0)

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

所以,让我们谈谈regexp:

1)([^_]*) - 在任何计数中抓住()(可能为零)*符号[],这不是^此符号{{1 }}

2)_ - 成为或不成为(_[_0-9a-z]*)?并抓住?序列,其中包含起始符号()和序列尾部,其中出现任何计数{{ 1}}来自_ *[]

_集中的任何符号