我正试图使用preg_split在PHP中将字符串拆分为术语。我需要提取普通单词(\w),还需要提取货币(甚至货币符号)和数字术语(包括逗号和小数点)。有人能帮我吗,因为我似乎无法创建一个有效的regex来用于preg_split来实现这一点。感谢
为什么不使用preg_match_all()
而不是preg_split()
?
$str = '"1.545" "$143" "$13.43" "1.5b" "hello" "G9"'
. ' This is a test sentence, with some. 123. numbers'
. ' 456.78 and punctuation! signs.';
$digitsPattern = '$?d+(.d+)?';
$wordsPattern = '[[:alnum:]]+';
preg_match_all('/('.$digitsPattern.'|'.$wordsPattern.')/i', $str, $matches);
print_r($matches[0]);
preg_match_all()
呢?每个单词都有这个[S]+b
,然后你就得到了一个包含单词的数组。
大棕狐狸-20.25美元将退还
preg_match_all('/[S]+b/', $str, $matches);
$matches = array(
[0] = 'Big',
[1] = 'brown',
[2] = 'fox',
[3] = '$20.25'
)
在空白处拆分是否解决了您的问题?"/s+/"