2017-05-30 48 views
0

你好,我有以下文字:如何使用正則表達式來獲取以下模式?

some text,+ 



this field is another parameter 


this is the final of the field 


t10681374flp 

t10681375flp 

我想下面兩行相匹配:

t10681374flp 
t10681375flp 

的規則是,說這些話的「T」開頭,以「P」結束, 我想:

grep -e t*p testing 

但是我:

this field is another parameter 
t10681374flp 
t10681375flp 

所以,我真的想欣賞支持,以克服這一任務,

+1

'grep -e^t。* p $ testing'應該這樣做。 –

+1

@詹姆斯布朗謝謝我真的很感謝支持 – neo33

回答

1

使用grep,避免匹配奇怪的線條和完美的比賽,下面

grep "^t[0-9]*flp$" testing 

這下面的線相匹配的代碼,

t10681374flp 
t10681375flp 

這並不線路匹配如下,

this field is another parameter 
these dont grep 

希望你能解決..

+0

這也會匹配'tflp' - 也就是't'後面沒有數字。不知道這是OP的意圖。爲了確保模式匹配't'後的至少一個數字,可以使用這個正則表達式:'^ t [0-9] [0-9] * flp $'。 – codeforester

0

之後應該做的工作:

grep ^t.*p$ testing 

^表示線條開始時,.*表示任何字符 和$指示結束線。