Extract hashtag from a string

Post your questions and help other users.

Moderator: Martin

Post Reply
User avatar
tsolignani
Posts: 187
Joined: 12 Jan 2019 11:53
Location: Vignola, Mo, Italy
Contact:

Extract hashtag from a string

Post by tsolignani » 04 Feb 2020 10:06

Good morning everyone.

Say a have a string like that «good morning #everyone, have a #nice day».

I would like to extract all hashtags, meaning all words prepended with «#».

How do I do that? I can search for the #character but then I should get the whole word, until the white space or comma or full stop or what.

Thank you.

User avatar
yogi108
Posts: 100
Joined: 09 Nov 2016 08:00
Contact:

Re: Extract hashtag from a string

Post by yogi108 » 04 Feb 2020 12:32

tsolignani wrote:
04 Feb 2020 10:06
Good morning everyone.

Say a have a string like that «good morning #everyone, have a #nice day».

I would like to extract all hashtags, meaning all words prepended with «#».

How do I do that? I can search for the #character but then I should get the whole word, until the white space or comma or full stop or what.

Thank you.
Hi,
You can try the following:
findAll('good morning #everyone, have a #nice day', '(?smi)(#\\w*)')
--> [#everyone, #nice]
I'm not a Regex expert, you can try also in the script Regex tester, there is also little help tutorial

Regards
Fritz
"You cannot know the meaning of your life until you are connected to the power that created you.”
Shri Mataji Nirmala Devi, founder of Sahaja Yoga

User avatar
tsolignani
Posts: 187
Joined: 12 Jan 2019 11:53
Location: Vignola, Mo, Italy
Contact:

Re: Extract hashtag from a string

Post by tsolignani » 04 Feb 2020 17:25

It does work! Thank you very much 👍

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Extract hashtag from a string

Post by Desmanto » 05 Feb 2020 18:54

I tes a bit and thought someone might already done it. So I just googled and found in stackoverflow : https://stackoverflow.com/questions/385 ... -semicolon

I use the answer from garyh.

Code: Select all

text = "Here's a #hashtag and here is #not_a_tag; which should be different. Also testing: Mid#hash. #123 #!@£ and <p>#hash</p>";

find = findAll(text, '(?<=[\\s>])#(\\d*[A-Za-z_]+\\d*)\\b(?!;)', true);
hashtag = newList();
for(i in find)
  addElement(hashtag, i[1]);
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

User avatar
tsolignani
Posts: 187
Joined: 12 Jan 2019 11:53
Location: Vignola, Mo, Italy
Contact:

Re: Extract hashtag from a string

Post by tsolignani » 05 Feb 2020 21:04

Very interesting, thank you!

Post Reply