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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:45:54+00:00 2026-06-15T19:45:54+00:00

I am trying to regex a character from a string that was assigned from

  • 0

I am trying to regex a character from a string that was assigned from an element of a scan result. I am trying to use match and it is complaining that the variable is an array. I am confused as to why “trash” is being seen as an array.

test = 'class="date">B=oddTu Q='
array = test.scan(/([A-Z])=/)
puts array
trash = array.last
trash.to_s
puts trash
if /Q/.match(trash)
  puts $1
end

And this is the results I’m seeing

C:\Ruby>scratch.rb
class="date">B=oddTu Q=
B
Q
Q
C:/Ruby/scratch.rb:14:in match: can't convert Array to String (TypeError)
        from C:/Ruby/scratch.rb:14:in `<main>'

EDIT: scan returns an array of array, so by doing trash = array.last, trash then gets taken down one level to 1 array. Doing trash = trash[0] then takes it down to a string.

  • 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-06-15T19:45:55+00:00Added an answer on June 15, 2026 at 7:45 pm

    As Boris points out, you’re getting an array of arrays. It’s because you have a group in your regex (a parenthesised expression). If you had several such groups, they would each correspond to an element in the returned arrays:

    test.scan(/([A-Z])(=)/)
    # => [["B", "="], ["Q", "="]]
    

    In your case there’s a few ways around this. You could simply flatten the array:

    test.scan(/([A-Z])=/).flatten
    # => ["B", "Q"]
    

    or you could use a positive lookahead instead of grouping the bit you’re interested in:

    test.scan(/[A-Z](?==)/)
    # => ["B", "Q"]
    

    Unfortunately, since the bit you’re not interested in is the = sign, it looks a bit weird with the lookahead syntax (?=pattern), so the flatten option might be clearer.

    • 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 a string that starts with a <p>
I'm trying to use regex to check if a full address string contains a
I have a string of text that I am trying to remove from a
I'm trying I have a string that I pass into my function from user
I'm trying to remove repeating white-space characters from UTF8 string in PHP using regex.
I'm trying to build a regex from a string object, which happens to be
I'm trying to add a character limit clause to this white list regex approach
trying to use regex to replace any white space with &nbsp;, inside of example
I am trying to use regex generators to create an expression, but I can't
I am trying to learn regex. I have the string: $x = 5ft2inches; How

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.