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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:17:41+00:00 2026-05-17T21:17:41+00:00

I know, I know, now I have two problems ‘n all that, but regex

  • 0

I know, I know, now I have two problems ‘n all that, but regex here means I don’t have to write two complicated loops. Instead, I have a regex that only I understand, and I’ll be employed for yonks.

I have a string, say stack.overflow.questions[0].answer[1].postDate, and I need to get the [0] and the [1], preferably in an array. “Easy!” my neurons exclaimed, just use regex and the split method on your input string; so I came up with this:

String[] tokens = input.split("[^\\[\\d\\]]");

which produced the following:

[, , , , , , , , , , , , , , , , [0], , , , , , , [1]]

Oh dear. So, I thought, “what would replaceAll do in this instance?”:

String onlyArrayIndexes = input.replaceAll("[^\\[\\d\\]]", "");

which produced:

[0][1]

Hmm. Why so? I’m looking for a two-element string array that contains “[0]” as the first element and “[1]” as the second. Why does split not work here, when the Javadocs declare they both use the Pattern class as per the Javadoc?

To summarise, I have two questions: why does the split() call produce that large array with seemingly random space characters and am I right in thinking the replaceAll works because the regex replaces all characters not matching “[“, a number and “]”? What am I missing that means I expect them to produce similar output (OK that’s three, and please don’t answer “a clue?” to this one!).

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

    well from what I can see the split does work, it gives you an array that holds the string split for each match that is not a set of brackets with a digit in the middle.

    as for the replaceAll I think your assumption is right. it removes everything (replace the match with "") that is not what you want.

    From the API documentation:

    Splits this string around matches of
    the given regular expression.

    This method works as if by invoking
    the two-argument split method with the
    given expression and a limit argument
    of zero. Trailing empty strings are
    therefore not included in the
    resulting array.

    The string “boo:and:foo”, for example,
    yields the following results with
    these expressions:

    Regex     Result
    :     { "boo", "and", "foo" }
    o     { "b", "", ":and:f" }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't know if the is even remotely possible, but just now I have
I don't really have any problems with the way I'm rendering now, but I
So I know now that the debug assemblies have been intentionally left out of
I'm new to Ruby on Rails, I know things but now I have to
Now i know to use the method of float.Parse but have bumped into a
Language Integrated Query. Now I know that the acronyms are. I have seen C#
I have been using MySQL for 2 years now, yet I still don't know
I have played around with Scala for a while now, and I know that
I have one main outstanding issue, I know now how to databind to lists
I have asked What should i know about search engine crawling? Now i would

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.