开发者

Regular expressions: how to remove machine tags from my tags list

I've a s开发者_如何学Goequence of tags and I only need to remove those tags having the following structure:

*:*:*

They are machine tags such as: flickr:event:132394 and not user submitted tags. What regular expression should I use ?

fields[i] = fields[i].replaceAll(" ,.*:.*", "");

thanks


fields[i] = fields[i].replaceAll("\\w+:\\w+:\\w+", "");

if the words consist of letters and digits only. To be safer you can even say:

fields[i] = fields[i].replaceAll("[^:]+:[^:]+:[^:]+", "");

that will remove all characters that are not colon. The only problem is with the last section. How can you know that the last word is finished? There is no colon there. If for example you wish to remove all characters that are not whitespace, say:

fields[i] = fields[i].replaceAll("[^:]+:[^:]+:\\S+", "");


This should do it:

fields[i] = fields[i].replaceAll("\\w+:\\w+:\\w+", "");

(See my answer to your other question for explanation)

Or, if you also need to deal with commas (and perhaps white space), use this version:

fields[i] = fields[i].replaceAll("\\s*,?\\s*\\w+:\\w+:\\w+", "");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜