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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T13:01:17+00:00 2026-05-22T13:01:17+00:00

I am trying to parse a pattern with regular expressions in Ruby. The pattern

  • 0

I am trying to parse a pattern with regular expressions in Ruby. The pattern is something like,

<number>? <comma>? <number>? <term>*

where:

  • number is one or more digits
  • comma is ","
  • term is of the form [.*] or [^.*]

And I am trying to capture the numbers, and all the terms. To clarify, here are some examples of valid patterns:

5,50[foo,bar]
5,[foo][^apples]
10,100[baseball][^basketball][^golf]
,55[coke][pepsi][^drpepper][somethingElse]

In the first, I’d like to capture 5, 50, and [foo,bar]
In the second, I’d like to capture 5, [foo] and [^apples] and so on.

The pattern I came up with is:

/(\d+)?,?(\d+)?(\[\^?[^\]]+\])+/

but this only matches the numbers and the last term. If I remove the + at the end, then it only matches the first term.

  • 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-22T13:01:17+00:00Added an answer on May 22, 2026 at 1:01 pm

    Easiest solution that I can think of with minimal effort would probably be to just throw on an additional capture group by surrounding the group and the + that are already there, i.e.

    /(\d+)?,?(\d+)?((\[\^?[^\]]+\])+)/
    

    Also, you could probably simplify the \d expressions by just doing (\d*) instead of (\d+)?…

    EDIT

    Here’s the code used to test the above suggestions:

    matches = [ "5,50[foo,bar]",
                "5,[foo][^apples]",
                "10,100[baseball][^basketball][^golf]",
                ",55[coke][pepsi][^drpepper][somethingElse]"
              ]
    
    re = Regexp.new('(\d*),?(\d*)((\[\^?[^\]]+\])+)')
    
    matches.each do |match|
      m = re.match(match)
    
      puts "\nMatching: #{match}"
      puts "--------------------"
    
      puts "Match 1: #{m[1]}"
      puts "Match 2: #{m[2]}"
      puts "Match 3: #{m[3]}"
    end
    

    and the output:

    Matching: 5,50[foo,bar]
    --------------------
    Match 1: 5
    Match 2: 50
    Match 3: [foo,bar]
    
    Matching: 5,[foo][^apples]
    --------------------
    Match 1: 5
    Match 2: 
    Match 3: [foo][^apples]
    
    Matching: 10,100[baseball][^basketball][^golf]
    --------------------
    Match 1: 10
    Match 2: 100
    Match 3: [baseball][^basketball][^golf]
    
    Matching: ,55[coke][pepsi][^drpepper][somethingElse]
    --------------------
    Match 1: 
    Match 2: 55
    Match 3: [coke][pepsi][^drpepper][somethingElse]
    

    Edit 2

    If you’re wanting tokenization, as per J-_-L’s suggestion with the scan method, add in:

    m[3].scan(/\[\^?[^\]]+\]/)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use regex to match digits in java, something like: Pattern p
I'm trying to parse strings that represent source code, something like this: [code lang=html]
Trying to parse Maplines for an airport. Each airport can have X number of
I'm trying to parse a grammar in ocamlyacc (pretty much the same as regular
I'm trying to parse a bit of HTML and I'd like to extract the
I'm very bad with regular expressions and cannot find how to write the pattern
I am trying to parse some C# code to Python. There are several regular
I'm trying to build a regular expression that matches regular expressions between two forward
I'm trying to parse command line arguments in an F# application. I'm using pattern
I am trying to parse xml to java classes, can any one help me.

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.