0

我试过这个,但它似乎只适用于字母数字字符?

\ba\w*\b  <- works
\b-\w*\b  <- doesn't work

如何从以连字符开头的字符串中删除所有单词?

4

1 回答 1

0

preg_replace("/-\w+/", "", "-000 aaa -bbb ccc -ddd -eee")

gives: aaa ccc


EDITED 3 times: As noticed in the comments - be it a stretch of the original question or not - all the suggested variants didn't work with -xx-yy-zz or qq-ww-ee, or -äüö. This one :

preg_replace("/(\s?)(?<=\s|^)(-)\S*\s*/", "$1", "-000 aaa -bbb ccc -ddd -eee-eee-eee -äüö fff-qqq-rrr äüö2 äüö-äüö-3 -ggg");

... produced the following:

 aaa ccc fff-qqq-rrr äüö2 äüö-äüö-3

Limitations apply: accented characters are acceptable; multibyte ones would certainly be a stretch.

于 2012-10-21T09:16:29.183 回答