小编tch*_*ist的帖子

Python:在单词边界上拆分unicode字符串

我需要一个字符串,并将其缩短为140个字符.

目前我在做:

if len(tweet) > 140:
    tweet = re.sub(r"\s+", " ", tweet) #normalize space
    footer = "… " + utils.shorten_urls(post['url'])
    avail = 140 - len(footer)
    words = tweet.split()
    result = ""
    for word in words:
        word += " "
        if len(word) > avail:
            break
        result += word
        avail -= len(word)
    tweet = (result + footer).strip()
    assert len(tweet) <= 140
Run Code Online (Sandbox Code Playgroud)

所以这对于英语非常有用,而英语就像字符串一样,但对于中文字符串来说却失败了,因为tweet.split()只返回一个数组:

>>> s = u"?????????????????????????????10?42???????????30?????????????????????????"
>>> s
u'\u7b80\u8baf\uff1a\u65b0\u83ef\u793e\u5831\u9053\uff0c\u7f8e\u570b\u7e3d\u7d71\u5967\u5df4\u99ac\u4e58\u5750\u7684\u300c\u7a7a\u8ecd\u4e00\u865f\u300d\u5c08\u6a5f\u665a\u4e0a10\u664242\u5206\u9032\u5165\u4e0a\u6d77\u7a7a\u57df\uff0c\u9810\u8a08\u7d0430\u5206\u9418\u5f8c\u62b5\u9054\u6d66\u6771\u570b\u969b\u6a5f\u5834\uff0c\u958b\u5c55\u4ed6\u4e0a\u4efb\u5f8c\u9996\u6b21\u8a2a\u83ef\u4e4b\u65c5\u3002'
>>> s.split()
[u'\u7b80\u8baf\uff1a\u65b0\u83ef\u793e\u5831\u9053\uff0c\u7f8e\u570b\u7e3d\u7d71\u5967\u5df4\u99ac\u4e58\u5750\u7684\u300c\u7a7a\u8ecd\u4e00\u865f\u300d\u5c08\u6a5f\u665a\u4e0a10\u664242\u5206\u9032\u5165\u4e0a\u6d77\u7a7a\u57df\uff0c\u9810\u8a08\u7d0430\u5206\u9418\u5f8c\u62b5\u9054\u6d66\u6771\u570b\u969b\u6a5f\u5834\uff0c\u958b\u5c55\u4ed6\u4e0a\u4efb\u5f8c\u9996\u6b21\u8a2a\u83ef\u4e4b\u65c5\u3002']
Run Code Online (Sandbox Code Playgroud)

我应该怎么做才能处理I18N?这在所有语言中都有意义吗?

如果重要的话,我正在使用python 2.5.4.

python unicode internationalization character-properties

10
推荐指数
3
解决办法
6617
查看次数

Perl,使用tr函数将大写转换为小写,反之亦然?

我有一个字符串

$string= 'AbCdEf';
Run Code Online (Sandbox Code Playgroud)

我想使用tr函数将所有大写字母转换为小写字母,将所有小写字母转换为大写字母....同时.我基本上只想扭转它成为.

aBcDeF
Run Code Online (Sandbox Code Playgroud)

我提出了这条线,但我不知道如何修改它来做我想要的.有什么帮助吗?

$string=~ tr/A-Z/a-z/;
Run Code Online (Sandbox Code Playgroud)

谢谢!

perl case-conversion

10
推荐指数
3
解决办法
3万
查看次数

python中字符的Unicode块

有没有办法在python中获取角色的Unicode块?该unicodedata模块似乎并不有我需要什么,我找不到一个外部库吧.

基本上,我需要与Character.UnicodeBlock.of()java中相同的功能.

python unicode character-properties

9
推荐指数
1
解决办法
4048
查看次数

嵌套的正则表达式前瞻和后瞻

我在正则表达式中嵌套的'+'/' - 'lookahead/lookbehind有问题.

假设我想改变'*'一个字符串,'%'让我们说它'\'逃脱了下一个字符.(将正则表达式转换为sql,如命令^^).

所以字符串

  • '*test*'应改为'%test%',
  • '\\*test\\*'- > '\\%test\\%',但是
  • '\*test\*''\\\*test\\\*'应保持不变.

我试过了:

(?<!\\)(?=\\\\)*\*      but this doesn't work
(?<!\\)((?=\\\\)*\*)    ...
(?<!\\(?=\\\\)*)\*      ...
(?=(?<!\\)(?=\\\\)*)\*  ...
Run Code Online (Sandbox Code Playgroud)

在上面给出的例子中,正确的正则表达式与'*'相匹配是什么?

是什么区别(?<!\\(?=\\\\)*)\*(?=(?<!\\)(?=\\\\)*)\*,或者如果这些人基本上是错误的有这样的可视化构造正则表达式的区别?

regex perl lookahead lookbehind regex-lookarounds

9
推荐指数
3
解决办法
4140
查看次数

cp dir recursivly不包括2个子目录

我有1个目录,包含9个子目录和10个文件.子目录具有下一级子目录和文件.

/home/directory/
/home/directory/subdirectory1
/home/directory/subdirectory2
...
/home/directory/subdirectory9
/home/directory/file1
...
/home/directory/file10
Run Code Online (Sandbox Code Playgroud)

我想递归复制所有子目录和文件,但不包括:

/home/directory/subdirectory5
/home/directory/subdirectory7
Run Code Online (Sandbox Code Playgroud)

最好的方法是什么?

unix linux bash cp

9
推荐指数
3
解决办法
2万
查看次数

Perl,为什么@INC不同?

我有一个简单的Perl脚本,打印出来@INC如下:

#!/usr/bin/perl
print $_, "\n" for @INC;
Run Code Online (Sandbox Code Playgroud)

我用两种不同的方式执行脚本./test.plperl test.pl,输出如下所示:

[neevek@~/bin]$ ./test.pl 
/Library/Perl/5.12/darwin-thread-multi-2level
/Library/Perl/5.12
/Network/Library/Perl/5.12/darwin-thread-multi-2level
/Network/Library/Perl/5.12
/Library/Perl/Updates/5.12.3
/System/Library/Perl/5.12/darwin-thread-multi-2level
/System/Library/Perl/5.12
/System/Library/Perl/Extras/5.12/darwin-thread-multi-2level
/System/Library/Perl/Extras/5.12
.   
[neevek@~/bin]$ perl test.pl 
/opt/local/lib/perl5/site_perl/5.12.3/darwin-multi-2level
/opt/local/lib/perl5/site_perl/5.12.3
/opt/local/lib/perl5/vendor_perl/5.12.3/darwin-multi-2level
/opt/local/lib/perl5/vendor_perl/5.12.3
/opt/local/lib/perl5/5.12.3/darwin-multi-2level
/opt/local/lib/perl5/5.12.3
/opt/local/lib/perl5/site_perl
/opt/local/lib/perl5/vendor_perl
.   
Run Code Online (Sandbox Code Playgroud)

我的问题是:用./script.pl和执行perl脚本的幕后操作是perl script.pl什么?是什么导致脚本输出不同@INC

perl library-path

9
推荐指数
2
解决办法
2607
查看次数

Perl替换运算符可以匹配数组中的元素吗?

我有这样的数组

my @stopWords = ("and","this",....)
Run Code Online (Sandbox Code Playgroud)

我的文字在这个变量中

my $wholeText = "....and so this is...."
Run Code Online (Sandbox Code Playgroud)

我想匹配标量wholeText中我的stopWords数组的每个元素的每一个出现,并用空格替换它.

一种方法如下:

foreach my $stopW (@stopWords)
{
   $wholeText =~ s/$stopW/ /;
}
Run Code Online (Sandbox Code Playgroud)

这适用于并替换所有停用词的每次出现.我只是想知道,如果有更短的方法.

像这样:

$wholeText =~ s/@stopWords/ /;
Run Code Online (Sandbox Code Playgroud)

以上似乎不起作用.

perl

8
推荐指数
3
解决办法
4805
查看次数

在Java中获取语言的unicode字符

Java中是否有任何方法可以获取特定语言的所有Unicode字符(例如孟加拉语或阿拉伯语)?

java unicode character-properties

8
推荐指数
2
解决办法
5082
查看次数

重音不敏感的正则表达式

我的代码:

jQuery.fn.extend({
 highlight: function(search){
  var regex = new RegExp('(<[^>]*>)|('+ search.replace(/[.+]i/,"$0") +')','ig');

  return this.html(this.html().replace(regex, function(a, b, c){
   return (a.charAt(0) == '<') ? a : '<strong class="highlight">' + c + '</strong>';
  }));
 }

});
Run Code Online (Sandbox Code Playgroud)

我想突出显示带重音的字母,即:

$('body').highlight("cao");
Run Code Online (Sandbox Code Playgroud)

应突出显示:[ção] OR [çÃo] OR [cáo] OR expre [cão] tion或[Cáo] tion

我怎样才能做到这一点?

regex unicode jquery highlight diacritics

8
推荐指数
1
解决办法
2439
查看次数

使用\ d扫描字符串中的Unicode数字

根据Oniguruma文档,\d字符类型匹配:

decimal digit char
Unicode:General_Category - Decimal_Number

但是,\d在包含所有Decimal_Number字符的字符串中扫描会导致仅匹配拉丁0-9位数:

#encoding: utf-8
require 'open-uri'
html = open("http://www.fileformat.info/info/unicode/category/Nd/list.htm").read
digits = html.scan(/U\+([\da-f]{4})/i).flatten.map{ |s| s.to_i(16) }.pack('U*')

puts digits.encoding, digits
#=> UTF-8
#=> 0123456789?????????????????????????????????????????????????????…

p RUBY_DESCRIPTION, digits.scan(/\d/)
#=> "ruby 1.9.2p180 (2011-02-18) [i386-mingw32]"
#=> ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
Run Code Online (Sandbox Code Playgroud)

我误读了文档吗?为什么不\d匹配其他Unicode数字,和/或有没有办法让它这样做?

ruby regex unicode character-properties

8
推荐指数
1
解决办法
1333
查看次数