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 8304117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:44:47+00:00 2026-06-08T17:44:47+00:00

How can I improve this? the purpose of this code is to be used

  • 0

How can I improve this?

the purpose of this code is to be used in a method that captures a string of hash_tags #twittertype from a form – parse through the list of words and make sure all the words are separated out.

WORD_TEST = "123 sunset #2d2-apple,#home,#star #Babyclub, #apple_surprise #apple,cats    mustard#dog , #basic_cable safety #222 #dog-D#DOG#2D "
SECOND_TEST = 'orion#Orion#oRion,Mike'

This is my problem area RegXps…

_string_rgx = /([a-zA-Z0-9]+(-|_)?\w+|#?[a-zA-Z0-9]+(-|_)?\w+)/

add_pound_sign = lambda { |a| a[0].chr == '#' ? a : a='#' + a; a}

I don’t know that much Regular Expressions: hence the needed collect the first[element] from the result of the scan -> It yielded weird stuff but the first element was always what I wanted.

 t_word = WORD_TEST.scan(_string_rgx).collect {|i| i[0] }
 s_word = SECOND_TEST.scan(_string_rgx).collect {|i| i[0] }
 t_word.map! { |a| a = add_pound_sign.call(a); a }
 s_word.map! { |a| a = add_pound_sign.call(a); a }

The results are what I want. I just want insight from Ruby | Regex guru’s out there.

puts t_word.inspect

[ 
"#123", "#sunset", "#2d2-apple", "#home", "#star", "#Babyclub", 
"#apple_surprise", "#apple", "#cats", "#mustard", "#dog", 
"#basic_cable", "#safety", "#222", "#dog-D", "#DOG", "#2D"
]

puts s_word.inspect

[
"#orion", "#Orion", "#oRion", "#Mike"
]

Thanks in advance.

  • 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-08T17:44:50+00:00Added an answer on June 8, 2026 at 5:44 pm

    Lets unfold the regex:

    (
       [a-zA-Z0-9]+ (-|_)? \w+
       | #? [a-zA-Z0-9]+ (-|_)? \w+
    )
    

    ( begin capture group

    [a-zA-Z0-9]+ match one or more alphanumeric characters

    (-|_)? match a hyphen or an underscore and save. This group may fail

    \w+ match one or more “word” characters (alphanumeric + underscore)

    | OR match this:

    #? match optional # character

    [a-zA-Z0-9]+ match one or more alphanumeric characters

    (-|_)? match hyphen or underscore and capture. may fail.

    \w+ match one or more word characters

    ) end capature

    I’d rather write this regex like this;

    (#? [a-zA-Z0-9]+ (-|_)? \w+)
    

    or

    ( #? [a-zA-Z0-9]+ (-?\w+)? )
    

    or

    ( #? [a-zA-Z0-9]+ -? \w+ )
    

    (all are reasonably equivalent)

    You should note that this regex will fail on hashtags with unicode characters, eg #Ü-Umlaut, #façadeetc. You are also limited to a two-character minimum length (#a fails, #ab matches) and may have only one hyphen (#a-b-c fails / would return #a-b)

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

Sidebar

Related Questions

How can I improve this code? What has made this long winded is the
I have read that you can do it, but would this really improve performance
Would like to know how I can improve this code to enable no page
How can I improve this username/password checking? [AcceptVerbs(HttpVerbs.Post)] public ActionResult Login(FormCollection collection) { var
Can this MS-Access query be improved? i.e. the SELECT TOP 1 col FROM Table2
I have an idea of how I can improve the performance with dynamic code
If I use String.intern() to improve performance as I can use == to compare
I've hacked together some php code that can probably be improved in terms of
Can someone help me improve on this? There has to be a better way.
I am trying to do something similar to How can I improve this SQL

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.