仙乃日 发表于 2018-12-16 17:15:22

字符串替换[已解决]

本帖最后由 仙乃日 于 2018-12-17 12:56 编辑

$a=",,高,,,,A,,B,,,,S,,"
$b="高,A,B,S,"
有什么好方法将字符串$a转换成$b的样子?
具体要求是替换后的字符串的开头不能有逗号,中间无论有几个逗号只保留1个。

bfgxp 发表于 2018-12-16 18:03:22

两次正则替换吧
StringRegExpReplace(StringRegExpReplace(",,高,,,,A,,B,,,,S,,","\,+","\,"),"^\,+","")

zghwelcome 发表于 2018-12-16 19:32:44

本帖最后由 zghwelcome 于 2018-12-16 19:36 编辑



StringRegExpReplace(',,高,,,,A,,B,,,,S,,','^,+|[,](?![^,])','')

fuldho 发表于 2018-12-16 21:49:58

$a=",,高,,,,A,,B,,,,S,,"
$b = StringRegExpReplace($a,"^,+|,+$"," ")
$b = StringRegExpReplace($b,",+",",")
MsgBox(0, "提示", $b)

水木子 发表于 2018-12-17 08:54:24

StringRegExpReplace(',,高,,,,A,,B,,,,S,,', '^,+|(?<=,),+', '')

syzeng 发表于 2018-12-20 16:50:01

真是高手啊,好好学习

haijie1223 发表于 2018-12-20 21:38:55

StringRegExpReplace(',,高,,,,A,,B,,,,S,,',',*([^,]+),*','$1,')
页: [1]
查看完整版本: 字符串替换[已解决]