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.
Extract hashtag from a string
Moderator: Martin
- tsolignani
- Posts: 187
- Joined: 12 Jan 2019 11:53
- Location: Vignola, Mo, Italy
- Contact:
Re: Extract hashtag from a string
Hi,tsolignani wrote: ↑04 Feb 2020 10:06Good 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.
You can try the following:
I'm not a Regex expert, you can try also in the script Regex tester, there is also little help tutorialfindAll('good morning #everyone, have a #nice day', '(?smi)(#\\w*)')
--> [#everyone, #nice]
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
Shri Mataji Nirmala Devi, founder of Sahaja Yoga
- tsolignani
- Posts: 187
- Joined: 12 Jan 2019 11:53
- Location: Vignola, Mo, Italy
- Contact:
Re: Extract hashtag from a string
It does work! Thank you very much
Re: Extract hashtag from a string
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.
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.
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.
- tsolignani
- Posts: 187
- Joined: 12 Jan 2019 11:53
- Location: Vignola, Mo, Italy
- Contact:
Re: Extract hashtag from a string
Very interesting, thank you!