2013-03-11 87 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