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

  • SEARCH
  • Home
  • 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 893825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:16:56+00:00 2026-05-15T14:16:56+00:00

I’m a newbie to ruby, I want to know if I can use just

  • 0

I’m a newbie to ruby, I want to know if I can use just one line to do the job.

Take the ‘search’ of this site for example. When user typed [ruby] regex, I can use following code to get the tag and keyword

'[ruby] regex' =~ /\[(.*?)\](.*)/
tag, keyword = $1, $2

Can we write it just in one line?


UPDATE

Thank you so much! May I make it harder and more interesting, that the input may contains more than one tags, like:

[ruby] [regex] [rails] one line

Is it possible to use one line code to get the tags array and the keyword? I tried, but failed.

  • 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-05-15T14:16:57+00:00Added an answer on May 15, 2026 at 2:16 pm

    You need the Regexp#match method. If you write /\[(.*?)\](.*)/.match('[ruby] regex'), this will return a MatchData object. If we call that object matches, then, among other things:

    • matches[0] returns the whole matched string.
    • matches[n] returns the nth capturing group ($n).
    • matches.to_a returns an array consisting of matches[0] through matches[N].
    • matches.captures returns an array consisting of just the capturing group (matches[1] through matches[N]).
    • matches.pre_match returns everything before the matched string.
    • matches.post_match returns everything after the matched string.

    There are more methods, which correspond to other special variables, etc.; you can check MatchData‘s docs for more. Thus, in this specific case, all you need to write is

    tag, keyword = /\[(.*?)\](.*)/.match('[ruby] regex').captures
    

    Edit 1: Alright, for your harder task, you’re going to instead want the String#scan method, which @Theo used; however, we’re going to use a different regex. The following code should work:

    # You could inline the regex, but comments would probably be nice.
    tag_and_text = / \[([^\]]*)\] # Match a bracket-delimited tag,
                     \s*          # ignore spaces,
                     ([^\[]*) /x  # and match non-tag search text.
    input        = '[ruby] [regex] [rails] one line [foo] [bar] baz'
    tags, texts  = input.scan(tag_and_text).transpose
    

    The input.scan(tag_and_text) will return a list of tag–search-text pairs:

    [ ["ruby", ""], ["regex", ""], ["rails", "one line "]
    , ["foo", ""], ["bar", "baz"] ]
    

    The transpose call flips that, so that you have a pair consisting of a tag list and a search-text list:

    [["ruby", "regex", "rails", "foo", "bar"], ["", "", "one line ", "", "baz"]]
    

    You can then do whatever you want with the results. I might suggest, for instance

    search_str = texts.join(' ').strip.gsub(/\s+/, ' ')
    

    This will concatenate the search snippets with single spaces, get rid of leading and trailing whitespace, and replace runs of multiple spaces with a single space.

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

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.