2013-03-11 89 views
2

我正在寻找解码正则表达式。 有没有一种方法来检查下面的正则表达式的意思:解码正则表达式

^(PC([Y\\d])|GC([Y\\d])|Y|\\d)\\d{4,5}$ 

回答

10

你可以在http://www.myezapp.com/apps/dev/regexp/show.ws使用正则表达式分析器或http://www.debuggex.com/


^ = start of string 
() = capturing groups 
[] = character classes 
\d = digit in 0-9 
\\ = literal backslash 
| = OR 
{} = count of leading item 
$ = end of string 
+0

对不起。我对正则表达式很陌生。你能帮我理解在正则表达式中分组的意义。 – user179516 2013-03-11 19:49:29

+0

@ user179516您遇到哪个特定部位? – 2013-03-11 19:55:48

+0

PC([])和GC([])组看起来与我相似。我想了解为什么我们在这里增加了两组。 – user179516 2013-03-11 20:04:55

3

这里是一个向下突破的发生了什么。

正则表达式:^(PC([Y\\d])|GC([Y\\d])|Y|\\d)\\d{4,5}$

1.^  - Beginning of line 
2. (  - Beginning of a capture group 
3. PC  - Finds `PC` exactly 
4. ([Y\\d]) - Creates a capture group for a Y or a single digit (0-9) 
5. |  - This is an OR statement 
6. GC  - Finds `GC` exactly 
7. ([Y\\d]) - Same as 4 
8. |  - This is an OR statement 
9. Y  - Finds `Y` exactly 
10. |  - This is an OR statement 
11. \\d  - This looks for a single digit (0-9) 
12.)  - End of capture group. Lines 3-11 will be in this capture group 
13. \\d{4,5} - This will look any digit exactly 4 or 5 times 
14. $  - End of line 

有这3个捕获组:

1. (PC([Y\\d])|GC([Y\\d])|Y|\\d) 
2. ([Y\\d]) (The first one) 
3. ([Y\\d]) (The second one) 

这里,我只是用123456来说明如何有效匹配(任意数量会被发现的列表有很多地方可以):

  • PCY1234
  • PCY12345
  • PCY1234
  • PCY12345
  • PC12345
  • PC123456
  • GC12345
  • GC123456
  • GCY1234
  • GCY12345
  • Y1234
  • Y12345

Here是具有用于每个匹配捕获基团的解释来RegExr的链接。

此外,中双\的原因是为了逃避用于Java的\。并不是所有的语言都需要这个,据我所知,有一些需要3.如果你注意到上面的RegExr,我删除了它们,RegExr会正确解析正则表达式。

+0

+1非常详细! – 2013-03-12 19:22:08

+0

谢谢,这样做是我教我自己的正则表达式:) – Nick 2013-03-12 22:32:36