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

  • Home
  • SEARCH
  • 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 5934121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:04:10+00:00 2026-05-22T15:04:10+00:00

I just started work on a C# application. I need to break down a

  • 0

I just started work on a C# application. I need to break down a string into different parts. Is there an easy way to do this using C# patterns? I think I can do it with substrings but it might get messy and I want to do something that’s easy to understand. Here’s an example of the input:

AB-CDE-GHI-123-45-67-7777

variable1 = "AB-CDE-GHI"
variable2 = "123"
variable3 = "45"
variable4 = "67"
variable5 = "67-7777"

AB-CDE-GHIJKLM-123-45-67-7777

variable1 = "AB-CDE-GHIJKLM"
variable2 = "123"
variable3 = "45"
variable4 = "67"
variable5 = "67-7777"

AB-123-45-67-7777

variable1 = "AB"
variable2 = "123"
variable3 = "45"
variable4 = "67"
variable5 = "67-7777"

The first part of the string up until “123-45-67-7777” can be any length. Lucky for me the last part 123-45-67-7777 is always the same length and contains numbers that are zero padded.

I hope someone can come up with some suggestions for an easy method that uses regular expressions or something.

Input lines look like this:

aa-123-45-67-7777
HJHJH-123-45-67-7777
H-H-H--123-45-67-7777
222-123-45-67-7777
  • 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-22T15:04:11+00:00Added an answer on May 22, 2026 at 3:04 pm

    You do not need RegEx for parsing this kind of input.

    You can use string.Split, in particular if the input is highly structured.

    If you first split by - you will get a string[] with each part in a different index of the array.

    The length property of the array will tell you how many parts you got and you can use that to reconstruct the parts you need.

    You can rejoin any of the bit you need back.

    string[] parts = "AB-CDE-GHI-123-45-67-7777".split('-');
    
    // joining together the first 3 items:
    string letters = string.Format("{0}-{1}-{2}", parts[0], parts[1], parts[2]);
    
    // letters = "AB-CDE-GHI"
    

    If the number of sections is variable (apart from the last 4), you can use the length in a loop to rebuild the wanted parts:

    StringBuilder sb = new StringBuilder();
    for(int i = 0; i < parts.Length - 4; i++)
    {
      sb.FormatAppend("{0}-", parts[i]);
    }
    sb.Length = sb.Length - 1; // remove trailing -
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just started getting into BizTalk at work and would love to keep using
We've just started using LINQ to SQL at work for our DAL & we
Have just started using Google Chrome , and noticed in parts of our site,
I've just started working on a brownfield ASP.NET 2.0 Web Application. As with a
I have just started working on my first Android application and am going ok.
Just started getting a bunch of errors on our C# .Net app that seemed
I just started thinking about creating/customizing a web crawler today, and know very little
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I just started using GNU Emacs as my text editor and I am concerned
I just started using the WPF WebBrowser that is included in Net 3.5 SP1.

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.