2017-01-31 59 views
1

我有一個日誌文件看起來像:「的電子郵件IP」正則表達式到日誌文件

'User_001','Entered server','[email protected]','2','','','0','YES','0','0',','0','192.168.1.1','192.168.1.2','0','0','0','0','0','0','0','0','0','1','0','','0','0','0','1' 
'User_002','Entered server','[email protected]','2','','','0','NO','0','0',','0','192.168.1.3','192.168.1.4','0','0','0','0','0','0','0','0','0','1','0','','0','0','0','1' 

OR

User_001 Entered server [email protected] 2 Pool_1 YES 0 0 0 192.168.1.1 192.168.1.2 0 0 0 0 0 0 0 0 0 1 0 0 1 
User_002 Entered server [email protected] 2 Pool_1 NO 0 0 0 192.168.1.3 192.168.1.4 0 0 0 0 0 0 0 0 0 1 0 0 1 

我試圖使出口正則表達式中的「電子郵件IP」格式化內容。

我像一個正則表達式的嘗試:

([A-Za-z0-9._%+-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,6}(.*)([0-9]{1,3}[\.]){3}[0-9]{1,3}) 

但是,當然,因爲這也得到了2個匹配的字符串之間的整個內容不工作。

如何忽略找到的兩個字符串之間的內容?

我試圖否定那個正則表達式部分沒有成功。

感謝大家提前!

P.s.我需要做這個用grep

+0

這看起來像一個帶引號的CSV那麼爲什麼不使用CSV解析器? –

+0

這是一個純文本文件,而不是csv:/ –

+2

CSV文件是一個純文本文件,其中包含按固定順序排列的值,以逗號分隔。情況並非如此嗎? –

回答

0

這是我的醜正則表達式的解決方案(即作品):

([a-z0-9][email protected][a-z0-9.]+).*?([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}) 

https://www.regex101.com/r/APfJS1/1

const regex = /([a-z0-9][email protected][a-z0-9.]+).*?([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})/gi; 
 
const str = `User_001','Entered server','[email protected]','2','','','0','YES','0','0',','0','192.168.1.1','192.168.1.2','0','0','0','0','0','0','0','0','0','1','0','','0','0','0','1'`; 
 
let m; 
 

 
while ((m = regex.exec(str)) !== null) { 
 
    // This is necessary to avoid infinite loops with zero-width matches 
 
    if (m.index === regex.lastIndex) { 
 
     regex.lastIndex++; 
 
    } 
 
    
 
    // The result can be accessed through the `m`-variable. 
 
    m.forEach((match, groupIndex) => { 
 
     console.log(`Found match, group ${groupIndex}: ${match}`); 
 
    }); 
 
}

但由於在評論中提到:一好的csv解析器可能會更好!

PHP

$re = '/([a-z0-9][email protected][a-z0-9.]+).*?([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})/i'; 
$str = 'User_001\',\'Entered server\',\'[email protected]\',\'2\',\'\',\'\',\'0\',\'YES\',\'0\',\'0\',\',\'0\',\'192.168.1.1\',\'192.168.1.2\',\'0\',\'0\',\'0\',\'0\',\'0\',\'0\',\'0\',\'0\',\'0\',\'1\',\'0\',\'\',\'0\',\'0\',\'0\',\'1\''; 

preg_match_all($re, $str, $matches); 

// Print the entire match result 
print_r($matches); 
+0

也許我做錯了什麼,這與PHP的作品,但如果我需要使用相同的正則表達式與grep失敗返回整個比賽再次。 示例命令i launch:grep -E -o「([a-z0-9] + @ [a-z0-9。] +)。*?([0-9] {1,3} \。[0 -9] {1,3} \。[0-9] {1,3} \。[0-9] {1,3})「 a.txt –

+0

我將添加PHP等價物,但您從未指定在你的問題中的語言! –

+0

ehm ..以及我需要在grep –

相關問題