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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:11:42+00:00 2026-05-31T21:11:42+00:00

I have a string seperated by \t and , , but the number of

  • 0

I have a string seperated by \t and ,, but the number of \t is not fixed, for example :

a=["seg1\tseg2\t\tseg3,seg4"]

seg2 and seg3 is seperated by two \t.

So I try to split them by

a.split(/\t+|,/)

it print the right anwser :

["seg1", "seg2", "seg3", "seg4"]

And I also try this

a.split(/[\t+,]/)

but the answer is

["seg1", "seg2", "", "seg3", "seg4"]

Why ruby print different results?

  • 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-31T21:11:43+00:00Added an answer on May 31, 2026 at 9:11 pm

    Because \t+ inside [] does not mean “one or more tabs”, it means “a tab or a plus”. Since it finds two consecutive tabs, it splits twice, and the string in the middle becomes empty.

    Most special characters, like . + * ? etc, when placed in an interval become “regular” characters. There are some exceptions, like ^ (which negates the interval when placed at the beginning), the \ (that escapes the next character(s), just like it does outside intervals) and the ] (that closes the interval; another [ is also disallowed there). So, [\t+,] actually means '\t' or '+' or ','.

    Unfortunatly, I don’t know any reference for the full set of characters that need or don’t need escaping inside an interval. In doubt, I tend to escape just to be sure. In any case, an interval will always match a single character only, if you want something different you must put your quantifier outside the interval. (For example: [\t,]+, if you also admit two commas in a row; otherwise, your first regex is really the correct one)

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

Sidebar

Related Questions

I'm trying to count the number of 0s in a string of numbers. Not
I have a string which has a version number. I want to read the
Binding checkbox in WPF is common issue, but I am still not finding example
I have a string of values separated by a space that I return to
I have a string named itemIDs separated by commas(12,43,34,..) and in order to use
Hi I have a string of numbers separated by commas, 100,200,300,400,500 that I'm splitting
I have string like this /c SomeText\MoreText Some Text\More Text\Lol SomeText I want to
I have string that I want to chop to array of substrings of given
I have string like \LESSING\root\cimv2:Win32_UserAccount.Domain=LESSING,Name=Admin How to convert it to LESSING\Admin using Framework?
I have string like this: G:\Projects\TestApp\TestWeb\Files\Upload\file.jpg How can I remove all text before Files

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.