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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:26:56+00:00 2026-05-26T09:26:56+00:00

I have a string: s = <aaa>bbb</ccc> I want to get aaa and bbb

  • 0

I have a string:

s = "<aaa>bbb</ccc>"

I want to get aaa and bbb in ruby block for sub method.
If I call:

s.sub(/<([a-z]+)>([\s\S]+)<\/[a-z]+>/,"first=\\1 second=\\2")

everything works as I expect, so the output is “first=aaa second=bbb”.
Then I call the same regexp with ruby-block, but it returns only the whole string but not \\1 and \\2 parts:

s.sub(/<([a-z]+)>([\s\S]+)<\/[a-z]+>/) { |x,y| puts x; puts y; }

This case output is

<aaa>bbb</ccc>, nil. 

How can I get sentences in braces like \\1, \\2 in ruby-block for sub method?

  • 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-26T09:26:57+00:00Added an answer on May 26, 2026 at 9:26 am

    The string yielded by sub will always be the full match. To get at the captures, you can use $1 and $2 inside the block.

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

Sidebar

Related Questions

Here is the sample string like '/aaa/bbb/ccc/ddd' or '/aaa/zzz' I want to get a
I have the next URL: http://domen.com/aaa/bbb/ccc . How can I get the string after
Suppose I have a string AAA BBB CCC DDD EEE FFF. How can I
I have a very complex string, such as: <p>aaa <font style=color:red>ABCD@@@EFG^&*))*T*^[][][]</p> <p>bbb <font style=color:red>ABCD@@@EFG^&*))*T*^[][][]</p>
is it possible to map a string field with a particular format like: aaa,bbb,ccc,ddd
I have dropdown list as follow: <select id=DropdownID> <option value=1>Ccc</option> <option value=2>Aaa</option> <option value=3>Bbb</option>
<string name=AAAAA>Select a item</string> <string-array name=planets_array> <item>AAA</item> <item>BBB</item> <item>CCC</item> <item>DDD</item> <item>EEE</item> <item>FFF</item> </string-array> I
I have a string like this: aaa bbb There is a space before the
I have a large Set<String> that contains many words, say: aaa, cCc, dDD, AAA,
String a=aaa; String b=bbb; String c=ccc; String d=ddd; String p,q,r,s; How can I assign

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.