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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:14:35+00:00 2026-06-14T12:14:35+00:00

I can split a string into two based on 2 spaces: string Line =

  • 0

I can split a string into two based on 2 spaces:

string Line = "1  2";

Regex.Split(Line, "  ");

=> 1, 2

I would like to add an exception. Only split if ‘not enclosed by [ ]’ as shown in this example.

string Line = "1  2  [1  2]";

Regex.Split(Line, "  ");

=> 1, 2, [1 2]

Can I fairly easily achieve this via regex? By the way, I use .NET.

  • 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-14T12:14:36+00:00Added an answer on June 14, 2026 at 12:14 pm

    You could use a lookahead, that asserts that there is no closing ] before the next opening [ or the end of the string:

    Regex.Split(Line, @"[ ]+(?![^\[\]]*\])");
    

    This will fail you if you have nested [...] structures though. Note that the lookahead is not part of the actual match, it just checks what follows without consuming anything. Inside the lookahead I used [^\[\]] which is a negated character class, matching any character except for any kind of square bracket.

    Also note that this splits on 1 or more spaces. If you want to require at least two, replace [ ]+ with [ ]{2,} and if you want exactly two with [ ]{2}.

    Further reading on lookarounds.

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

Sidebar

Related Questions

I would like to split string into List on two possible delimeters / or
How can we split a std::string and a null terminated character array into two
I have a string like google.com in Python, which I would like split into
How can I split a string such as Mar10 into Mar and 10 in
I'm trying to split a string into two different words. I have the following
I have a bunch of lines that I'd like to split into two columns,
How can I split up a string into pieces? For example, how can I
using only regular expression methods, the method String.replaceAll and ArrayList how can i split
Can split(string, array, separator) in awk use sequence of whitespaces as the separator (or
Given a string such as: 23,234,456 first second third How can I split string

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.