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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:49:11+00:00 2026-05-26T12:49:11+00:00

I have a string like so: @[30:Larry Middleton] I want to return just 30.

  • 0

I have a string like so:

"@[30:Larry Middleton]"

I want to return just 30. Where 30 will always be digits, and can be of 1 to infinity in length.

I’ve tried:

user_id = result.match(/@\[(\d+):.*]/)

But that returns everything. How can I get back just 30?

  • 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-26T12:49:12+00:00Added an answer on May 26, 2026 at 12:49 pm

    If that’s really all your string, you don’t need to match the rest of the pattern; just match the consecutive integers:

    irb(main):001:0> result = "@[30:Larry Middleton]"
    #=> "@[30:Larry Middleton]"
    irb(main):002:0> result[/\d+/]
    #=> "30"
    

    However, if you need to match this as part of a larger string that might have digits elsewhere:

    irb(main):004:0> result[/@\[(\d+):.*?\]/]
    #=> "@[30:Larry Middleton]"
    irb(main):005:0> result[/@\[(\d+):.*?\]/,1]
    #=> "30"
    irb(main):006:0> result[/@\[(\d+):.*?\]/,1].to_i
    #=> 30
    

    If you need the name also:

    irb(main):002:0> m = result.match /@\[(\d+):(.*?)\]/
    #=> #<MatchData "@[30:Larry Middleton]" 1:"30" 2:"Larry Middleton">
    irb(main):003:0> m[1]
    #=> "30"
    irb(main):004:0> m[2]
    #=> "Larry Middleton"
    

    In Ruby 1.9 you can even name the matches, instead of using the capture number:

    irb(main):005:0> m = result.match /@\[(?<id>\d+):(?<name>.*?)\]/
    #=> #<MatchData "@[30:Larry Middleton]" id:"30" name:"Larry Middleton">
    irb(main):006:0> m[:id]
    #=> "30"
    irb(main):007:0> m[:name]
    #=> "Larry Middleton"
    

    And if you need to find many of these:

    irb(main):008:0> result = "First there was @[30:Larry Middleton], age 17, and then there was @[42:Phrogz], age unknown."
    #irb(main):015:0> result.scan /@\[(\d+):.*?\]/
    #=> [["30"], ["42"]]
    irb(main):016:0> result.scan(/@\[(\d+):.*?\]/).flatten.map(&:to_i)
    #=> [30, 42]
    irb(main):017:0> result.scan(/@\[(\d+):(.*?)\]/).each{ |id,name| puts "#{name} is #{id}" }
    Larry is 30
    Phrogz is 42
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have string like this /c SomeText\MoreText Some Text\More Text\Lol SomeText I want to
I have string like this: G:\Projects\TestApp\TestWeb\Files\Upload\file.jpg How can I remove all text before Files
How can we divide the substring from the string Like I have string String
I have string looking like this: 'Toy Story..(II) (1995)' I want to split the
I have string like this 0100110011001 I want to convert it to byte array
I have string like order by o desc,b asc Here I want to replace
I have string like the following 'TextBoxKunde85_3_0', and i want to extract only the
I have String like Dec 31, 1969 4:00:00 PM. I want to convert it
I have string like [any characters]{$\prime$}[any characters]$[any characters]{$\prime$}[any characters]$[any characters] I want to replace
I have string like Rupeshposts-this-question I need a regular expression which can check whether

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.