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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:51:12+00:00 2026-06-10T03:51:12+00:00

upfront the code to visualize a bit the problem I am facing: This is

  • 0

upfront the code to visualize a bit the problem I am facing:

  1. This is the text that needs to be split.
:20:0444453880181732
:21:0444453880131350
:22:CANCEL/ABCDEF0131835055
:23:BUY/CALL/E/EUR
:82A:ABCDEFZZ80A
:87A:4444655604
:30:061123
:31G:070416/1000/USNY
:31E:070418
:26F:PRINCIPAL
:32B:EUR1000000,00
:36:1,31000000
:33B:USD1310000,00
:37K:PCT1,60000000
:34P:061127USD16000,00
:57A:ABCDEFZZ80A

This is my Regex

 Regex r = new Regex(@"\:\d{2}\w*\:", RegexOptions.Multiline);

 MatchCollection matches = r.Matches(Content);
 string[] items = r.Split(Content);

 // ----- Fix for first entry being empty string.
 int index = items[0] == string.Empty ? 1 : 0;

 foreach (Match match in matches)
 {
    MessageField field = new MessageField();

    field.FieldIdExtended = match.Value;
    field.Content = items[index];

    Fields.Add(field);

    index++;
 }

As you can see from the comments the problem occurs with the splitting of the string.
It returns as first item an empty string.
Is there any elegant way to solve this?

Thanks, Dimi

  • 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-10T03:51:13+00:00Added an answer on June 10, 2026 at 3:51 am

    The reason that you are getting this behaviour is that your first delimiter from the split has nothing before it and this the first entry is blank.

    The way to solve this properly is probably to capture the value that you want in the regular expression and then just get it from your match set.

    At a rough first guess you probably want something like:

    Regex r = new Regex(@"^:(?<id>\d{2}\w*):(?<content>.*)$", RegexOptions.Multiline);
    
    MatchCollection matches = r.Matches(Content);
    
    foreach (Match match in matches)
    {
        MessageField field = new MessageField();
    
        field.FieldIdExtended = match.Groups["id"].ToString()
        field.Content = match.Groups["content"].ToString();
    
        Fields.Add(field);
    
    }
    

    The use of named capture groups makes it easy to extract stuff. You may need to tweak the regex to be more as you want. Currently it gets 20 as id and 0444453880181732 as content. I wasn’t 100% clear on what you needed to capture but you look ok with regex so I assume that isn’t a problem. 🙂

    Essentially here you are not really trying to split stuff but match stuff and pull it out.

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

Sidebar

Related Questions

This question is a bit long due the source code, which I tried to
I apologize upfront for my lack of jquery knowledge. In this website I am
Up front: This is my first attempt at an Android app. I'm in that
How do you folks retrieve all objects in code upfront? I figure you can
I have this code to concate some array elements: StringBuilder sb = new StringBuilder();
This is perhaps not a question unique to Mercurial, but that's the SCM that
I have a helper inside a partial that I am loading something like this:
I asked this question before but the problem is I get one response and
In our SVN-Code Repository, I came across a package specification that -after removing a
When writing application code, it's generally accepted that premature micro -optimization is evil, and

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.