开发者

Deal with '#' through regex

开发者 https://www.devze.com 2023-03-12 20:44 出处:网络
Quick question , I have been trying to match any word containing a \'#\' from a string list and remove it, but I don\'t know how to handle it . been playing around on http://regexhero.net/tester/ tryi

Quick question , I have been trying to match any word containing a '#' from a string list and remove it, but I don't know how to handle it . been playing around on http://regexhero.net/tester/ trying but to no avail.

Essentially if it comes across #ff or wh开发者_Python百科a#s up i will just regex.replace them.

any ideas on the Regular expression to use?.

Thanks.


Don't use regex - just use string.replace - it's a lot faster.


I have a previous answer that covers some hashtag matching approaches.

In summary, if you are pulling statuses containing hashtags from Twitter, you no longer need to find them yourself. You can now specify the include_entities parameter to have Twitter automatically call out mentions, links, and hashtags (if the method you are calling, like statuses/show supports this parameter.

If you just need the regular expression to locate the hashtags and capture it's elements, Twitter provides it in an open source library that contains the following pattern.

(^|[^0-9A-Z&/]+)(#|\uFF03)([0-9A-Z_]*[A-Z_]+[a-z0-9_\\u00c0-\\u00d6\\u00d8-\\u00f6\\u00f8-\\u00ff]*)

More detail and additional links are provided in the original answer.


So you're trying to remove any words containing a #?

If so, give this a try...

\w*#\w*

And replace with nothing, like so...

http://regexhero.net/tester/?id=cda1e713-bdab-4aa2-b63d-a87e9b2c9bce

apple# orange ban#ana becomes orange


But if you're simply trying to remove all instances of #, then String.Replace is the better choice. myString = myString.Replace("#", "");

0

精彩评论

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

关注公众号