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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:23:12+00:00 2026-06-14T06:23:12+00:00

I have to translate this preg_split code into a Java equivalent. I have tried

  • 0

I have to “translate” this preg_split code into a Java equivalent. I have tried a few things without success. Here’s the PHP code I am attempting to translate:

$str="1 [3 4 5] 6 7 [8 9] 4";
$chars = preg_split("^\[(.*?)\]||/ /^", $str, -1, PREG_SPLIT_DELIM_CAPTURE);

foreach ($chars as $key => $value) {
    if(ord($value)!=0 && $value!=" "){
        $res[]=$value;
    }
}
print_r($res);

As you can see, my String input can be composed of any sequence of number chars, some of them being wrapped with [ ] brackets:

1 [3 4 5] 6 7 [8 9] 4

Brackets can be either at the beginning or end of the String, there’s no restriction on that.

Following the example above, result would look like this:

[0] = "1"
[1] => "3 4 5"
[2] => "6"
[3] => "7"
[4] => "8 9"
[5] => "4"

I’ve found some issues trying to translate the regular expression (looks like “\” fail as invalid escape sequence).

  • 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-14T06:23:14+00:00Added an answer on June 14, 2026 at 6:23 am

    Rather than thinking of the problem as “splitting” I would try and come up with a regular expression that matches any single substring of the form you are looking for, then gather all matches of that expression, for example:

    Pattern p = Pattern.compile("(?<=\\[)[^\\]]+|\\d+");
    Matcher m = p.matcher(inputStr);
    List<String> matches = new ArrayList<String>();
    while(m.find()) matches.add(m.group());
    

    The bit to the left of the | matches anything between square brackets, the bit to the right matches any other sequence of digits.

    Note that when you want to write a regular expression in a Java string literal you need to double the backslashes – the literal "\\" represents a single character string containing one backslash.

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

Sidebar

Related Questions

I want to translate this java code into equal objective-c code double[] pMatrix =
I have never used ruby but need to translate this code to java. Can
Can someone please translate this high-level code into IA-32 assembly without using any global
I have this link which i need to translate in a php variable. http://www.facebook.com/dialog/apprequests?app_id=346824075388300&to=1149862205&message=Facebook+Dialogs+are+so+easy%21&redirect_uri=http%3A%2F%2Fpenelope-ns.net%2Ffb%2F
I have to translate some code from C# to Java and I have the
i want to translate this line of code from php to asp.net $subid[$value['parentid']][] =
I have this code: $str = '(Test)'; $final = preg_replace('/\[translate=([a-z]{2})(.*)\]'.preg_quote($matches[3][$i]).'\[\/translate(.*)\]/',$str,$final,-1,$ct); It handles a situation
I am trying to translate this piece of code from Java to C# and
I have to translate this C code to assembly code: #include <stdio.h> int main(){
Help me translate this into proper NHibernate... I have an Oracle database with 2

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.