4 に答える
1
入力がUTF-8であると仮定すると、
'/(\pL+)/u'
文字列内の\pL+
1つ以上の文字に一致します。
例:
$str = '彼はそこに ひと人 でいた。';
preg_match_all('/(\pL+)/u', $str, $matches);
var_dump($matches[0]);
出力:
array(3) {
[0]=>
string(15) "彼はそこに"
[1]=>
string(9) "ひと人"
[2]=>
string(9) "でいた"
}
于 2011-08-21T17:16:17.617 に答える
1
I think this: /([^ 、]+)/
should match the words in examples you've given (you may want to add some other word-terminating chars apart from space and 、 if you have them in your texts (or use \pL
instead of [^ 、]
to cover all UTF letters.
EXAMPLE
<?
preg_match_all('/[^ 、]+/u', "彼らは日本の 国民 となった。", $m);
print_r($m);
outputs
Array
(
[0] => Array
(
[0] => 彼らは日本の
[1] => 国民
[2] => となった。
)
)
于 2011-08-21T12:51:57.667 に答える
0
you're trying only to split your string according to some pattern (white space, or punctuation), is that true?? what about this?
In [51]: word = '.test test\n.test'
In [53]: re.split('[\s,.]+',word)
Out[53]: ['', 'test', 'test', 'test']
于 2011-08-21T13:07:58.613 に答える
0
于 2011-08-22T09:44:28.117 に答える