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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T14:50:13+00:00 2026-05-17T14:50:13+00:00

I have some strings that I would like to pattern match and then extract

  • 0

I have some strings that I would like to pattern match and then extract out the matches as variables $1, $2, etc.

The pattern matching code I have is

a = /^([\+|\-]?[1-9]?)([C|P])(?:([\+|\-][1-9]?)([C|P]))*$/i.match(field)

puts result = #{a.to_a.inspect}

With the above I am able to easily match the following sample strings:

“C”, “+2C”, “2c-P”, “2C-3P”, “P+C”

And I have confirmed all of these work on the Rubular website.
However, when I try to match “+2P-c-3p”, it matches however, the MatchData “array-like object” looks like this:

result = ["+2P-C-3P", "+2", "P", "-3", "P"]

The problem is that I am unable to extract into the array, the middle pattern “-C”.

What I would expect to see is:

result = ["+2P-C-3P", "+2", "P", "-", "C", "-3", "P"]

It seems to extract only the end part “-3P” as “-3” and “P”

Does anyone know how I can modify my pattern to capture the middle matches ?
So as an other example, +3c+2p-c-4p, I would expect should create:

["+3c+2p-c-4p", "+3", "C", "+2", "P", "-", "C", "-4", "P"]

but what I get is

["+3c+2p-c-4p", "+3", "C", "-4", "P"]

which completely misses the middle part.

  • 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-17T14:50:14+00:00Added an answer on May 17, 2026 at 2:50 pm

    This is what I managed to do :

    ([+-]?\d?)(C|P)(?=(?:[+-]?\d?[CP])*$)
    

    This way you capture multiple elements.
    The only problem is the validity of the string. As ruby doesn’t have look-behind I can’t check the start of the string, so zerhyju+2P-C-3P is valid (but will only capture +2P-C-3P) whereas +2P-C-3Pzertyuio isn’t valid.

    If you want to both capture and check if your string is valid, the best way (IMO) is to use two regexes, one to check the value ^(?:[+-]?\d?[CP])*$ and a second one to capture ([+-]?\d?)(C|P) (You could also use ([CP]) for the last part).

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

Sidebar

Related Questions

I have some strings that I am pulling out of a database and I
I have some strings, entered by users, that may look like this: ++7 7++
For one NSString, I have N pattern strings. I'd like to extract substrings around
I have a service with some entities that I would like to expose in
I have a string that has some Environment.Newline in it. I'd like to strip
I have this .NET regex: ^(?<prefix>([^]*))\s(?<attrgroup>(\([^\)]*\)))\s(?<suffix>([^]*))$ It properly matches the following strings: some prefix
I have some strings of xxh:yym format where xx is hours and yy is
I have to change some connection strings in an incredibly old legacy application, and
The Back Story I have some decimal values which I am displaying as strings
I have several strings in the rough form: [some text] [some number] [some more

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.