Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8437069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:20:08+00:00 2026-06-10T07:20:08+00:00

I am trying to extract hashtags for a simple college project using ruby on

  • 0

I am trying to extract hashtags for a simple college project using ruby on rails. I am facing issue with tags that include only numericals and with tags with no space.

text = "Pack my #box with #5 dozen liquor.#jugs link.com/liquor#jugs #2good #first#second"

The regex I have is /(?:^|\s)#(\w+)/i (source)

This regex returns #["box", "5", "2good", "first"]

How to make sure it only returns #["box", "2good"] and ignore the rest as they are not ‘real’ hashtags?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:20:10+00:00Added an answer on June 10, 2026 at 7:20 am

    Can you try this regex:

    /(?:^|\s)(?:(?:#\d+?)|(#\w+?))\s/i
    

    UPDATE 1:
    There are a few cases where the above regex will not match like: #blah23blah and #23blah23.
    Hence modified the regex to take care of all cases.

    Regex:

    /(?:\s|^)(?:#(?!\d+(?:\s|$)))(\w+)(?=\s|$)/i
    

    Breakdown:

    • (?:\s|^) –Matches the preceding space or start of line. Does not
      capture the match.
    • # –Matches hash but does not capture.
    • (?!\d+(?:\s|$))) –Negative Lookahead to avoid ALL numeric characters
      between # and space (or end of line)
    • (\w+) –Matches and captures all word characters
    • (?=\s|$) –Positive Lookahead to ensure following space or end of
      line. This is required to ensure it matches adjacent valid hash tags.

    Sample text modified to capture most cases:

    #blah Pack my #box with #5 dozen #good2 #3good liquor.#jugs
    link.com/liquor#jugs #mkvef214asdwq sd #3e4 flsd #2good #first#second #3

    Matches:

    Match 1: blah
    Match 2: box
    Match 3: good2
    Match 4: 3good
    Match 5: mkvef214asdwq
    Match 6: 3e4
    Match 7: 2good

    Rubular link

    UPDATE 2:

    To exclude words starting or ending with underscore, just include your exclusions in the negative lookahead like this:

    /(?:\s|^)(?:#(?!(?:\d+|\w+?_|_\w+?)(?:\s|$)))(\w+)(?=\s|$)/i
    

    The sample, regex and matches are recorded in this Rubular link

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to extract the Android /res folder into a separate project that's included
I am a Rails Newbie. I am trying to implement Hashtags in my project.
Trying to extract strings that are wrapped in double brackets. For example [[this is
im trying to extract some files from a jar-file downloaded using java-webstart. below code
I am trying to extract the video id from a youtube url using the
I am trying to extract some info from a website using simple_html_dom. Currently I
I'm trying to extract specific fields from my file. Basically, output fields only containing
I'm trying to extract all the zip files in a directory using MSBuild. Rather
I'm trying to extract all of the data out of heading tags in a
I'm trying to extract the token verification to a seperate method and after that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.