Detect hashtags and add to array

Detect hashtags and add to array

I have a UITextField where the users can write a description.

Example: "This is a image of my #car. A cool #sunshine background also for my #fans."

How can i detect the hashtags "car", "sunshine" and "fans", and add them to a array?


let frame = CGRect(x: 0.0, y: 0.0, width: 100.0, height: 30.0)

let description = UITextField(frame: frame)
description.text = "This is a image of my #car. A cool #sunshine background also for my #fans."


extension String {
    func getHashtags() -> [String]? {
        let hashtagDetector = try? NSRegularExpression(pattern: "#(\\w+)", options: NSRegularExpressionOptions.CaseInsensitive)
        let results = hashtagDetector?.matchesInString(self, options: NSMatchingOptions.WithoutAnchoringBounds, range: NSMakeRange(0, self.utf16.count)).map { $0 }

        return results?.map({
            (self as NSString).substringWithRange($0.rangeAtIndex(1))
        })
    }
}

description.text?.getHashtags() // returns array of hashtags

Source: https://github.com/JamalK/Swift-String-Tools/blob/master/StringExtensions.swift

JS: extract HashTags from text, hasgtag.js: /* Extract hashtags text from string as an array */ function getHashTags(inputText) { var regex = /(? test_hashtag.js: var expect = require('​chai').expect; describe('Test npm install mocha $ mocha test_hashtag.js  Contribute to connrs/node-find-hashtags development by creating an account on GitHub.


Swift 4.2 version. At the end we return a list of hashtags/keywords without #.

extension String {
func getHashtags() -> [String]? {
    let hashtagDetector = try? NSRegularExpression(pattern: "#(\\w+)", options: NSRegularExpression.Options.caseInsensitive)

    let results = hashtagDetector?.matches(in: self, options: .withoutAnchoringBounds, range: NSRange(location: 0, length: count))

    return results?.map({
        (self as NSString).substring(with: $0.range(at: 1)).capitalized
    })
}

}

e.g

Input #hashtag1 #hashtag2 #hashtag3 #hashtag4 #hashtag5

Output [hashtag1, hashtag2, hashtag3, hashtag4, hashtag5]

find-hashtags, Extracts the hashtags from a string. Travis CI Test Status. Get an array of hashtags extracted from a string. Install. npm i find-hashtags  This post about how to detect hashtag text and linkfy. like Instagram, twitter , google+ and facebook tags. Check the below example. Screen Shot


Check this pod: https://cocoapods.org/pods/twitter-text

In TwitterText class there is a method (NSArray *)hashtagsInText:(NSString *)text checkingURLOverlap (BOOL)checkingURLOverlap

Twitter created this pod for finding #, @, URLs so in my opinion there is no better way to do this. :)

‍ Parsing a tweet to extract hashtags into an array in Python, Note that, as suggested in other answers, this may also find non-hashtags in string: if char=='#': hashtag = True if s: ret.append(s) s='' continue # take only the  In order for others to find your corner of Instagram, hashtags have been designed to help create or allow you to be part of wider conversations online and make you more discoverable. Hashtags are the road signs that direct people to your Instagram Profile. Think of hashtags as your own Google keywords for people to discover you.


Swift 3 version of @Anurag's answer:

extension String {
func getHashtags() -> [String]? {
    let hashtagDetector = try? NSRegularExpression(pattern: "#(\\w+)", options: NSRegularExpression.Options.caseInsensitive)
    let results = hashtagDetector?.matches(in: self, options: NSRegularExpression.MatchingOptions.withoutAnchoringBounds, range: NSMakeRange(0, self.characters.count)).map { $0 }

    return results?.map({
        (self as NSString).substring(with: $0.rangeAt(1))
    })
}

}

Extract #Hashtag Keywords from a String of Text with PHP, The first function returns an array with all hashtags. PHP Add all matches to array */ Using our PHP function we'll return the following array: $string = "​This is a test using the hashtags of #WordPress and #Shortcode.";. We will look for Hashtags that are similar to the ones you entered. Add this page to your Homescreen/Bookmark if you find it useful! There are many hashtag suggestion pages out there and millions of peoples are using them. And that exactly is the problem because the hashtags on those sites rarely change so everybody uploads using the same hashtags.


Array of tokenized documents for text analysis - MATLAB, Detect complex tokens in text, such as web addresses, emoticons, emoji, and hashtags Add sentence and part-of-speech details using the addSentenceDetails and By default, the function treats the hashtag "#MATLAB" , the emoticon ":-D"  Other brands will add hashtags in a separate comment on the post, but this may cause the hashtags to lose effectiveness. For most brands, the best option is simply to make sure that your hashtags are separated by at least one line from the caption, and are hidden under the “See More” tag.


String parsing in Swift, partialName = nil return parse(character) } name.append(character) partialName Since detecting both usernames and hashtags is exactly the same We no longer have to maintain an array of symbols, // but we do need to  Input text, specified as a string array, character vector, cell array of character vectors, or cell array of string arrays. If the input text has not already been split into words, then str must be a string array, character vector, cell array of character vectors, or a cell array of string scalars.


How to model hashtags with nodejs and mongodb, Searching documents by hashtags is trivial if you just add an index, as well as inserting, updating for (HashTag tag: mongodb.hashtags.find()) { for (Image img:  Another common source of confusion is the difference between using length to get the size of an array and length() to get the size of a String. Notice the presence of parentheses when working with Strings. (array.length is a variable, while String.length() is a method specific to the String class.) Syntax: datatype[] var var[element] = value var.length