today's offers - xanax use in pregnancy xanax xr be abused soma internet pharmacies fedex soma without priscription fed ex phentermine parkinson drugs phentermine baclofen tramadol together ordering tramadol cod viagra find search 76k cialis pages cialis buy on line viagra for sale without a prescription buy onlinecom phentermine viagra ambien and klonopin drug interaction buy ambien overnight cheap online pharmacies valium saturday delivery valium online with no prescription or membership fioricet overnight no rx cheap fioricet without a prescription order meridia cod fedex meridia cheap fed ex delivery xanax bradycardia what does xanax look like american soma how to buy soma online without prescriptin phentermine overnight echeck c o d 375 mg phentermine rss feed cheap no prescription tramadol stop the tramadol generic prices online cialis generic cialis fedex chinese herbal viagra taking partial pill of viagra ambien and pregnancy smoking ambien cod online valium buy cheap valium without prescription no prescription fioricet with fedex fioricet without prescription in Brasilia order meridia meridia information

Posts Tagged regexp

VIM — Matching a line break 跨行比對 & Non-Greedy

Matching a line bread Doc
Non-Greedy Reference

<input name=”inst6″ type=”radio” value=”4″>
<a href=”javascript:;” onMouseOver=”MM_swapImage(‘q2′,”,’images/qa_p_HTC%20TyTN.jpg’,1)” onMouseOut=”MM_swapImgRestore()”><img src=”images/0-HTC%20TyTN.gif” name=”htc” width=”100″ height=”15″ border=”0″ id=”htc”></a>

因為想讓input radio 也有onMouseOver 的效果
所以想把<a href=”javascript:;” onMouseOver=”MM_swapImage(‘q2′,”,’images/qa_p_HTC%20TyTN.jpg’,1)” onMouseOut=”MM_swapImgRestore()”>也加在<input name=”inst6″ type=”radio” value=”4″>這個tag 的前面…然後後面再加</a>
因為很多行我不想一行一行傻傻的改所以…..就用regexp 了…

這邊包含了兩個技巧一個是non-greedy,一個是 Matching a line break
\(<input name=”inst[0-9]*.*>\)\(\_s\+\)\(<a href=”javascript:;.\{-}>\)/\3\1<\/a>\3/gc

這樣就可以全部換掉了…Orz
我沒仔細檢查過,剛剛看replace 完後是很work…..
::-p:

,

No Comments

Regexp (zero-width negative look-ahead)

剛剛在irc 看到的
(22:09:12) stormax: ‹一個正規式不知道怎樣寫, 我想搜尋 <? 但是要排除 <?php
(22:09:21) stormax: 有長輩知道怎麼寫嗎?
.
.
.
(22:31:19) mhsin: stormax: <\?(!php) # 22:09 < stormax> �一個正規式不知道怎樣寫, 我想搜尋 < ? 但是要排除
(22:31:19) yinjieh: 那我會 < ?php
(22:31:33) yinjieh: 再 phpphp -> php
(22:31:53) mhsin: stormax: (!something) 叫 zero-width negative look-ahead
(22:32:01) mhsin: 啊錯了
(22:32:06) mhsin: (?!something) 才對
(22:32:24) mhsin: aaa(?!bbb) 就會 match 「後面不是 bbb 的 aaa」
然後去google 了一下
就找到一篇
Perl Regular Expressions Tip Sheet
裡面就有講到
zero-width negative look-ahead
我也去grep 實做了一下~~
還真的work

$ grep -P ‘aaa(?!bbb)’ ok.php
看irc 還真不少東西 ::-p:

UPDATE:2007/11/08
新增有關VIM 上的用法

							*/zero-width*
	When using "\@=" (or "^", "$", "\<", "\>") no characters are included
	in the match.  These items are only used to check if a match can be
	made.  This can be tricky, because a match with following items will
	be done in the same position.  The last example above will not match
	"foobarfoo", because it tries match "foo" in the same position where
	"bar" matched.

	Note that using "\&" works the same as using "\@=": "foo\&.." is the
	same as "\(foo\)\@=..".  But using "\&" is easier, you don't need the
	braces.

							*/\@!*
\@!	Matches with zero width if the preceding atom does NOT match at the
	current position. |/zero-width| {not in Vi}
	Like '(?!pattern)" in Perl.
	Example			matches 
	foo\(bar\)\@!		any "foo" not followed by "bar"
	a.\{-}p\@!		"a", "ap", "app", etc. not followed by a "p"
	if \(\(then\)\@!.\)*$	"if " not followed by "then"

	Using "\@!" is tricky, because there are many places where a pattern
	does not match.  "a.*p\@!" will match from an "a" to the end of the
	line, because ".*" can match all characters in the line and the "p"
	doesn't match at the end of the line.  "a.\{-}p\@!" will match any
	"a", "ap", "aap", etc. that isn't followed by a "p", because the "."
	can match a "p" and "p\@!" doesn't match after that.

	You can't use "\@!" to look for a non-match before the matching
	position: "\(foo\)\@!bar" will match "bar" in "foobar", because at the
	position where "bar" matches, "foo" does not match.  To avoid matching
	"foobar" you could use "\(foo\)\@!...bar", but that doesn't match a
	bar at the start of a line.  Use "\(foo\)\@<!bar".

							*/\@<=*
\@<=	Matches with zero width if the preceding atom matches just before what
	follows. |/zero-width| {not in Vi}
	Like '(?<=pattern)" in Perl, but Vim allows non-fixed-width patterns.
	Example			matches 
	\(an\_s\+\)\@<=file	"file" after "an" and white space or an
				end-of-line
	For speed it's often much better to avoid this multi.  Try using "\zs"
	instead |/\zs|.  To match the same as the above example:
		an\_s\+\zsfile

	"\@<=" and "\@<!" check for matches just before what follows.
	Theoretically these matches could start anywhere before this position.
	But to limit the time needed, only the line where what follows matches
	is searched, and one line before that (if there is one).  This should
	be sufficient to match most things and not be too slow.
	The part of the pattern after "\@<=" and "\@<!" are checked for a
	match first, thus things like "\1" don't work to reference \(\) inside
	the preceding atom.  It does work the other way around:
	Example			matches 
	\1\@<=,\([a-z]\+\)	",abc" in "abc,abc"

							*/\@<!*
\@<!	Matches with zero width if the preceding atom does NOT match just
	before what follows.  Thus this matches if there is no position in the
	current or previous line where the atom matches such that it ends just
	before what follows.  |/zero-width| {not in Vi}
	Like '(?<!pattern)" in Perl, but Vim allows non-fixed-width patterns.
	The match with the preceding atom is made to end just before the match
	with what follows, thus an atom that ends in ".*" will work.
	Warning: This can be slow (because many positions need to be checked
	for a match).
	Example			matches 
	\(foo\)\@<!bar		any "bar" that's not in "foobar"
	\(\/\/.*\)\@\<!in	"in" which is not after "//"

							*/\@>*
\@>	Matches the preceding atom like matching a whole pattern. {not in Vi}
	Like '(?>pattern)" in Perl.
	Example		matches 
	\(a*\)\@>a	nothing (the "a*" takes all the "a"'s, there can't be
			another one following)

	This matches the preceding atom as if it was a pattern by itself.  If
	it doesn't match, there is no retry with shorter sub-matches or
	anything.  Observe this difference: "a*b" and "a*ab" both match
	"aaab", but in the second case the "a*" matches only the first two
	"a"s.  "\(a*\)\@>ab" will not match "aaab", because the "a*" matches
	the "aaa" (as many "a"s as possible), thus the "ab" can't match.

No Comments