2013-03-22 67 views
0

我有一個像下面的文字。如何用正則表達式選擇特定數量的字符單詞

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum 
has been the industry's standard dummy text ever since the fivec harword 1500s, when an unknown printer 
took a galley of type and scrambled it to make a type specimen fivec harword book. It has survived not 
only five centuries, but also the leap into electronic typesetting, remaining essentially 
unchanged. It was popularised in the 1960s with the release of fivec harword Letraset sheets containing 
Lorem Ipsum passages, and more recently with desktop publishing software like Aldus 
PageMaker including versions of Lorem Ipsum. 

這就是我需要的正則表達式:

1-選擇五個字符的單詞。

2-在第一步之後選擇一個空格。

3-在第二步之後選擇七個字符。

它應該捕獲所有的fivec harword字符串。我怎樣才能做到這一點?

回答

2

使用這一個:

\b\w{5}\s\w{7}\b 

解釋:

The regular expression: 

(?-imsx:\b\w{5}\s\w{7}\b) 

matches as follows: 

NODE      EXPLANATION 
---------------------------------------------------------------------- 
(?-imsx:     group, but do not capture (case-sensitive) 
         (with^and $ matching normally) (with . not 
         matching \n) (matching whitespace and # 
         normally): 
---------------------------------------------------------------------- 
    \b      the boundary between a word char (\w) and 
          something that is not a word char 
---------------------------------------------------------------------- 
    \w{5}     word characters (a-z, A-Z, 0-9, _) (5 
          times) 
---------------------------------------------------------------------- 
    \s      whitespace (\n, \r, \t, \f, and " ") 
---------------------------------------------------------------------- 
    \w{7}     word characters (a-z, A-Z, 0-9, _) (7 
          times) 
---------------------------------------------------------------------- 
    \b      the boundary between a word char (\w) and 
          something that is not a word char 
---------------------------------------------------------------------- 
)      end of grouping 
---------------------------------------------------------------------- 
+0

感謝它的作品,我的預期。 – Imrahil 2013-03-22 11:15:35

+0

@Ihrahil:不客氣。 – Toto 2013-03-22 11:18:46

+0

+1對於一個不好的問題很好的答案 – 2014-09-08 19:00:07

1

這應該做的伎倆

(^|\W)\w{5}\s\w{7}($|\W) 

(^|\W)開始的字符串或一個非文字字符的。

\w{5}的5個字字符

\s一個空格的字符串

\w{7}的7個單詞字符

字符串或一個非文字字符

如果的

($|\W)端串特別要在字符串周圍留出空格(而不是標點符號等)將\W替換爲\s

+0

這將不匹配在開始時或在字符串的末尾。 – Toto 2013-03-22 11:11:29

+0

爲什麼這個不起作用?我添加了5個字符的正則表達式結束。 '\ W \ w {5} \ s \ w {7} \ s \ w {5} \ W'我試着用適當的文字來嘗試。 – Imrahil 2013-03-22 11:14:07

+0

M42是正確的,我已經調整過我的字符串開頭和結尾(如果需要,仍然允許靈活地匹配空格) – 2013-03-22 11:15:44

0

試試這個

\b[a-zA-Z]{5}\s[][a-zA-Z]{7}\b 

\ b表示邊界

[A-ZA-Z]所有的α投注

{5} 5個字符與先前表達

\ S單個空格

相關問題