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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:46:25+00:00 2026-05-30T18:46:25+00:00

str = cruel world #pattern can be /(?<a>.)(?<b>.)/ OR /(?<b>.)(?<a>.)/ #which was inputted by

  • 0
str = "cruel world"
#pattern can be /(?<a>.)(?<b>.)/   OR   /(?<b>.)(?<a>.)/   
#which was inputted by user, we don't know which one will be picked up by user.
pattern = params[:pattern]

please using str.scan(/#{pattern}/) or other match methods expect output:

 p a 
# ["c", "u", "l", "w", "r"]
  p b 
# ["r", "e", " ", "o", "l"]
  p c 
# [ ]
# there is no named group: `?<c>`  in this case. However, we should take it if user inputted.

This is my solution:

str = "cruel world"
#case 1
pattern =  /(?<a>.)(?<b>.)/ 
a = Array.new
b = Array.new
c = Array.new
str.scan(/#{pattern}/) do |x|  
  a << Regexp.last_match(:a) if $~.names.include? "a" 
  b << Regexp.last_match(:b) if $~.names.include? "b" 
  c << Regexp.last_match(:c) if $~.names.include? "c" 
end
p a
p b
p c 

is there a better way?

  • 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-30T18:46:26+00:00Added an answer on May 30, 2026 at 6:46 pm

    Here’s my solution

    def find_named_matches(str, pattern)
      names = pattern.names
      return Hash[names.zip [[]] * names.size] unless str =~ pattern
      Hash[names.zip str.scan(pattern).transpose]
    end
    

    And tests

    describe 'find_named_matches' do
      example 'no matches' do
        find_named_matches('abcabcabc', /(?<a>x.)(?<b>.)/).should == {'a' => [], 'b' => []}
      end
    
      example 'simple match' do
        find_named_matches('abc', /(?<a>.)/).should == {'a' => %w[a b c]}
      end
    
      example 'complex name' do
        find_named_matches('abc', /(?<Complex name!>.)/).should == {'Complex name!' => %w[a b c]}
      end
    
      example 'two simple variables' do
        find_named_matches('cruel world', /(?<a>.)(?<b>.)/).should == 
          {'a' => %w[c u l w r], 'b' => %w[r e \  o l]}
      end
    
      example 'two simple variables' do
        find_named_matches('cruel world', /(?<b>.)(?<a>.)/).should ==
          {'b' => %w[c u l w r], 'a' => %w[r e \  o l]}
      end
    
      example "three variables and matched chars that aren't captured" do
        find_named_matches('afk1bgl2chm3', /(?<a>.)(?<f>.)(?<k>.)./).should ==
          {'a' => %w[a b c], 'f' => %w[f g h], 'k' => %w[k l m]}
      end
    
      example 'complex regex' do
        find_named_matches("the dog's hog is the cat's rat", /(?<nouns>(?:(?<=the |'s ))\w+)/).should ==
          {'nouns' => %w[dog hog cat rat]}
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

str = Hello☺ World☹ Expected output is: Hello:) World:( I can do this: str.gsub(☺,
string str = one three; string::iterator it; string add = two ; Lets say
str is of the following pattern: 1 abc (1 <something>) For example: 1 abc
see char str[] = hello world; printf(%s,str); printf statement prints the all character in
char *str = malloc (14); sprintf(str, %s, one|two|three); char *token1, *token2, *token3; char *start
var str = I hope ducks don't smile upon me whenever I pretend to
NSMutableString *str = [[NSMutableString alloc] init]; Suppose str has right know a value me.
When compiling this: char *str = [[NSString stringWithFormat:@%i days and %i hours, days, hours]
function escCtrlChars(str) { return str.replace(/[\0\t\n\v\f\r\xa0'!-]/g, function(c) { return '!' + c.charCodeAt(0) + '!'; });
static void Main() { string str; str = Console.ReadLine(); while (str != null)//HERE! {

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.