2013年5月9日 星期四

正規式搜尋應用

git log 中
搜尋除了tom.cn和mary.cn這兩個人以外的大陸開發者

$ git log
/\s.*[^(tom|mary)]\.cn\s
=> 結果可能有問題

疑問:
git grep出來的東西很長怎麼複製?
都要按->才會跑後面的內容那樣
現在都分段複製..
Rellik解法:
$ git grep "xxx" > grep.log
但我git設定關係在行尾會多藍色的^M ( http://sealmemory.blogspot.tw/2012/11/vim-m.html =>測試結果是\r\n非\n\r )
移除 ^M :%s/^M//g
 ^M 怎麼打出來的=> 先導斜線(\)->ctrl+v(按著ctrl)後按m 就能搜到藍色的^M(Windows中的換行)

Qilly: 不等於空跟5 要怎麼寫呀
http://stackoverflow.com/questions/3333461/regular-expression-which-matches-a-pattern-or-is-an-empty-string
^$|pattern
Ans: !~ /^(5|)$/
ex. $ perl -e "print 'true' if ('test_entries' =~ /^(5|)$/);"

搜尋不包含特定字串的一行
http://vim.wikia.com/wiki/Search_for_lines_not_containing_pattern#Using_the_:v_command
/^\(\(The_Regular_Expression\)\@!.\)*$
ex. 在 svn log 中搜尋有哪些檔案被修改,但不包含jpg檔
/^Index:\(\(jpg\)\@!.\)*$
http://stackoverflow.com/questions/96826/vim-how-do-i-exclude-an-entire-word-from-my-search
Question: 搜尋任何一句片語開頭為"abc "後面可以接任何東西,除了"defg" ( negative look-ahead assertion )
Answer:
/abc \(defg\)\@!
用 :help \\@! 查 {not in Vi} 如同Perl的 "(?!pattern)"

http://stackoverflow.com/questions/406230/regular-expression-to-match-string-not-containing-a-word
Question:
Hoho
Hihi
Haha
hede
要搜尋 hede以外的東西
Answer: (vim不適用,sublime可以)
^((?!hede).)*$ => the solution to does not contain “hede”
注意:不能用 ^(?!hede).*$ 因為這個解法 does not start with “hede”

上面兩個解法的差別:
vim: 用\@!放在裡面括號外後面且括號要跳脫
/^\(\(jpg\)\@!.\)*$
sublime: 用?!放在括號內前面,括號不用跳脫
^((?!hede).)*$

在字串中找最後一個符號(ex. 找路徑下的檔名)
http://stackoverflow.com/questions/3331970/regex-how-to-match-the-last-dot-in-a-string
([^\/]*)$  或 [^\/]*$

判斷大於零的整數
preg_match("/^[1-9][0-9]*$/",$id)




沒有留言:

張貼留言