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日 星期四

2013年3月6日 星期三

perl 判斷整數

http://bbs.chinaunix.net/thread-1596012-1-1.html

使用正規表示式:

$ perl -e 'my $xx=1 ;if ($xx=~/^-?\d+$/){warn "int";}else{warn "not int"};warn $xx'

判斷資料型態(數字、字串、refrence-hash、reference-array):
use Scalar::Util qw(looks_like_number);

my $val;
$val = { a => "123", b => "456" };
$val = [{ a => "123", b => "456" }];
$val = 6486;
$val = "string test"

if(ref($val)){
  print ref($val);
}else{
  print "is", looks_like_number($val) ? '' : ' not', " a number\n";
}

http://stackoverflow.com/questions/1731333/how-do-i-tell-what-type-of-value-is-in-a-perl-variable
使用ref()
http://stackoverflow.com/questions/12647/how-do-i-tell-if-a-variable-has-a-numeric-value-in-perl
使用 use Scalar::Util qw(looks_like_number);