2016-05-17 92 views
0

我需要一些幫助與跟進此格式的正則表達式:字符串的正則表達式,重複,次數羣

第一部分是一個電子郵件地址,隨後的8個列由";"分。

[email protected];Alex;Test;Alex A.Test;Alex;12;34;56;78 

第一部分我已經是(.*@.*com

這些也都是可能的來源字符串:

[email protected];Alex;;Alex A.Test;;12;34;56;78 
[email protected];Alex;;Alex A.Test;Alex;;34;;78 
[email protected];Alex;Test;;Alex;12;34;56; and so on 
+1

什麼語言是您使用? –

+1

最簡單的方法是用編程語言中的分號分割字符串。你在用什麼(這不是正則表達式)? –

+0

這是一個bash腳本,所以我會使用sed。 – calabash

回答

0

你可以像

".*@.*\.com;[A-Z,a-z]*;[A-Z,a-z]*;[A-Z,a-z, ,.,]*;[A-Z,a-z]*;[0-9][0-9];[0-9][0-9];[0-9][0-9];[0-9][0-9]" 

假設數字只有兩位數字

1

你可以試試這個正則表達式:

^(.*@.*com)(([^";\n]*|"[^"\n]*");){8}(([^";\n]*|"[^"\n]*"))$ 

如果你有不同數量的ADRESS後列的改變{}

之間的數

在這裏輸入內容:

1. `[email protected]` 
2. `56;` 
3. `56` 
4. `78` 

Here the test

如果你是肯定會有你的字符串沒有"您可以使用此:

^(.*@.*com)(([^;\n]*);){8}([^;\n]*)$ 

Here the test

編輯:

OP建議這種用法:
使用第一個正則表達式sed需要-i -n -E標誌並轉義字符"
結果將是這樣的:

sed -i -n -E "/(.*@.*com)(([^\";\n]*|\"[^\"\n]*\");){8}(([^\";\n]*|\"[^\"\n]*\"))/p" 
+0

這個爲我做的工作,謝謝。 sed命令必須如何才能在此模式之後僅打印行? – calabash

+0

使用正則表達式看看[這裏](http://unix.stackexchange.com/questions/78625/)。告訴你是否正在尋找。 –

+0

試試這個:'sed -i -E「/(.*@.*com)(([^\";\n]*|\"[^\"\n]*\");){8} (([^ \「; \ n] * | \」[^ \「\ n] * \」))/ p「' –