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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:30:40+00:00 2026-05-19T03:30:40+00:00

I have a string like this This:string:must~:be:split:when:previous:char:is:not~:this I need to split the line with

  • 0

I have a string like this

This:string:must~:be:split:when:previous:char:is:not~:this

I need to split the line with the delimiter “:” but only if the character before the delimiter is NOT “~”

I have the following regex now:

String[] split = str.split(":(?<!~:)");

It works, but since I arrived at it purely by trial and error, I’m not convinced that its the most efficient way of doing it. Also, this function will be repeatedly called on large strings frequently, so performance does come into consideration. What is a more efficient way of doing it?

  • 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-19T03:30:41+00:00Added an answer on May 19, 2026 at 3:30 am

    Update: To make this more fair I wanted to use a compiled Pattern and see the results of that. So I updated the code to use compiled pattern, non-compiled pattern and my custom method.

    While this isn’t using regex it proves to be faster then the regex given.

    public static void main(String[] args) {
        Pattern pattern = Pattern.compile(":(?<!~:)");
        for (int runs = 0; runs < 4; ++runs) {
            long start = System.currentTimeMillis();
            for (int index = 0; index < 100000; ++index) {
                "This:string:must~:be:split:when:previous:char:is:not~:this".split(":(?<!~:)");
            }
            long stop = System.currentTimeMillis();
            System.out.println("Run: " + runs + " Regex: " + (stop - start));
    
            start = System.currentTimeMillis();
            for (int index = 0; index < 100000; ++index) {
                pattern.split("This:string:must~:be:split:when:previous:char:is:not~:this");
            }
            stop = System.currentTimeMillis();
            System.out.println("Run: " + runs + " Compiled Regex: " + (stop - start));
    
            start = System.currentTimeMillis();
            for (int index = 0; index < 100000; ++index) {
                specialSplit("This:string:must~:be:split:when:previous:char:is:not~:this");
            }
            stop = System.currentTimeMillis();
            System.out.println("Run: " + runs + " Custom: " + (stop - start));
        }
    
        for (String s : specialSplit("This:string:must~:be:split:when:previous:char:is:not~:this")) {
            System.out.println(s);
        }
    }
    
    public static String[] specialSplit(String text) {
        List<String> stringsAfterSplit = new ArrayList<String>();
    
        StringBuilder splitString = new StringBuilder();
        char previousChar = 0;
        for (int index = 0; index < text.length(); ++index) {
            char charAtIndex = text.charAt(index);
            if (charAtIndex == ':' && previousChar != '~') {
                 stringsAfterSplit.add(splitString.toString());
                 splitString.delete(0, splitString.length());
            } else {
                    splitString.append(charAtIndex);
            }
                previousChar = charAtIndex;
        }
        if (splitString.length() > 0) {
            stringsAfterSplit.add(splitString.toString());
        }
        return stringsAfterSplit.toArray(new String[stringsAfterSplit.size()]);
    }
    

    Output

    Run: 0 Regex: 468
    Run: 0 Compiled Regex: 365
    Run: 0 Custom: 169
    Run: 1 Regex: 437
    Run: 1 Compiled Regex: 363
    Run: 1 Custom: 166
    Run: 2 Regex: 445
    Run: 2 Compiled Regex: 363
    Run: 2 Custom: 167
    Run: 3 Regex: 436
    Run: 3 Compiled Regex: 361
    Run: 3 Custom: 167
    This
    string
    must~:be
    split
    when
    previous
    char
    is
    not~:this
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this that I need to parse into a 2D
This probably has a simple answer, but I must not have had enough coffee
I have string like this /c SomeText\MoreText Some Text\More Text\Lol SomeText I want to
I have a string like this: string s = This is my string; I
I have a string which is like this: this is "a test" I'm trying
I have a string that looks like this: Name1=Value1;Name2=Value2;Name3=Value3 Is there a built-in class/function
I have a string of test like this: <customtag>hey</customtag> I want to use a
I have a text string structured like this: = Some Heading (1) Some text
Lets say I have an array like this: string [] Filelist = ... I
So I have something like this: var xmlStatement:String = xmlObject.node[3].@thisValue; What mystery function do

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.