2013年5月23日 星期四

windows perl安裝

NB重灌後因為win7沒有內建perl,所以重新安裝

遇到第一個問題,要安裝ActivePerl 還是Strawberry Perl

http://stackoverflow.com/questions/3365518/should-i-choose-activeperl-or-strawberry-perl-for-windows
雖然這篇最推薦的是ActivePerl,但因為下面那個答案說Strawberry 有CPAN,所以還是安裝了Strawberry


安裝好後在window cmd執行pl檔,發現utf8格式的檔案印中文時會出現亂碼
http://bbs.chinaunix.net/thread-1434560-1-1.html

windows如果要显示中文,编码就是cp936
如果你的文件是以UTF-8编码的话在一个cp936编码的cmd中打印出来就是一个乱码,你需要做的是一个从UTF-8到cp936的转化的,可以使用Encode模块的from_to函数来实现
notepad默认是用cp936编的,跟cmd的编码一样,所以不存在乱码。你可以用gvim打开,set encoding=utf8之后就能看到乱码了,因为编码不一致

解法1:
使用git bash執行pl檔(但原本的中文名稱的資料夾和檔案會亂碼)
解法2:

1、将test.pl文件内容复制粘贴到一个“新建文本文档.txt"中。(用系统自带的记事本打开,原来的test.pl是用gvim编辑的)
2、删除test.pl
3、将"新建文本文档.txt”改名为test.pl
4、執行pl檔



2013年5月22日 星期三

perl 中文字尾亂碼

Question:

http://i.imgur.com/VUHWREJ.jpg

最後一個字被切掉了

我想要把後面的亂碼移掉請問要怎麼做呢?

目前試過下面這方法... 字尾還是有亂碼...

use Encode;

$str # 字串內容為上面那張圖的字串

Encode::from_to($str,'UTF-8','UTF-8');

print $str; #結果還是一樣

Anwser:

先轉成unicode把特殊字元\x{fffd}濾掉,然後再轉回utf8這樣就可以了...

Encode::from_to($str,'UTF-8','unicode');
$str =~ s/\x{fffd}//g; #這邊g的意思是... I added a "g" after the last forward slash. The "g" stands for "global", which tells Perl to replace all matches, and not just the first one.  ( http://www.regular-expressions.info/perl.html )
Encode::from_to($str,'unicode','UTF-8');


其他資料:
http://stackoverflow.com/questions/6234386/how-do-i-sanitize-invalid-utf-8-in-perl
(第二個答案無效)
http://www.fileformat.info/info/unicode/char/fffd/index.htm
(查那個亂碼的unicode)
http://stackoverflow.com/questions/1016910/how-can-i-strip-invalid-xml-characters-from-strings-in-perl
http://www.perlmonks.org/?node_id=931058
(類似解法)

新方法:
http://www.ichiayi.com/wiki/tech/check_utf8



  
#!/usr/bin/perl

sub strip_non_utf8_characters {
    my $text=shift;
    my $utf8_rgx='\A(
     [\x09\x0A\x0D\x20-\x7E]            # ASCII
   | [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
   |  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
   | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
   |  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
   |  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
   | [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
   |  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
  )*\z';
  my $tlen=length($text);
  print "\n length:",$tlen;
  for(my $i=0;$i<$tlen;$i++){
    $text=substr($text,0,$tlen-$i);
    return $text if( $text=~ m/$utf8_rgx/x );
  }
  return '';
}

sub t{
  my $text=shift;
  for(my $i=0;$i< length($text) ;$i+=2){
    printf( "split length=%d response:%s\n",
      $i,
      &strip_non_utf8_characters(substr($text,0,$i))
    );
  }
}
$string = "歡迎來到全世界最大的網站";
#&t($string);
print "\n",substr($string,0,10),"\n";
print length $string;
print "\n",&strip_non_utf8_characters(substr($string,0,999)); #結果

  

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)




2013年4月26日 星期五

多行文本溢出显示省略号(...)的方法

http://c7sky.com/text-overflow-ellipsis-on-multilinea-text.html

现在的浏览器都支持text-overflow:ellipsis属性,用来实现单行文本的溢出显示省略号,但是这个属性并不支持多行文本。那么有没有方法在多行文本上实现同样的效果呢?


jQuery

除了各个浏览器私有的属性,有没有跨浏览器的解决方法呢?当然是通过js实现啦!(通过从后向前逐个删除末尾字符,直至元素的高度小于父元素高度)

$(".figcaption").each(function(i){
    var divH = $(this).height();
    var $p = $("p", $(this)).eq(0);
    while ($p.outerHeight() > divH) {
        $p.text($p.text().replace(/(\s)*([a-zA-Z0-9]+|\W)(\.\.\.)?$/, "..."));
    };
});

Demo: http://jsfiddle.net/Cople/DrML4/5/

2013年4月11日 星期四