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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:49:52+00:00 2026-05-15T06:49:52+00:00

I’m trying to do some parsing that will be easier using regular expressions. The

  • 0

I’m trying to do some parsing that will be easier using regular expressions.

The input is an array (or enumeration) of bytes.

I don’t want to convert the bytes to chars for the following reasons:

  1. Computation efficiency
  2. Memory consumption efficiency
  3. Some non-printable bytes might be complex to convert to chars. Not all the bytes are printable.

So I can’t use Regex.

The only solution I know, is using Boost.Regex (which works on bytes – C chars), but this is a C++ library that wrapping using C++/CLI will take considerable work.

How can I use regular expressions on bytes in .NET directly, without working with .NET strings and chars?

Thank you.

  • 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-15T06:49:53+00:00Added an answer on May 15, 2026 at 6:49 am

    There is a bit of impedance mismatch going on here. You want to work with Regular expressions in .Net which use strings (multi-byte characters), but you want to work with single byte characters. You can’t have both at the same time using .Net as per usual.

    However, to break this mismatch down, you could deal with a string in a byte oriented fashion and mutate it. The mutated string can then act as a re-usable buffer. In this way you will not have to convert bytes to chars, or convert your input buffer to a string (as per your question).

    An example:

    //BLING
    byte[] inputBuffer = { 66, 76, 73, 78, 71 };
    
    string stringBuffer = new string('\0', 1000);
    
    Regex regex = new Regex("ING", RegexOptions.Compiled);
    
    unsafe
    {
        fixed (char* charArray = stringBuffer)
        {
            byte* buffer = (byte*)(charArray);
    
            //Hard-coded example of string mutation, in practice you would
            //loop over your input buffers and regex\match so that the string
            //buffer is re-used.
    
            buffer[0] = inputBuffer[0];
            buffer[2] = inputBuffer[1];
            buffer[4] = inputBuffer[2];
            buffer[6] = inputBuffer[3];
            buffer[8] = inputBuffer[4];
    
            Console.WriteLine("Mutated string:'{0}'.",
                 stringBuffer.Substring(0, inputBuffer.Length));
    
            Match match = regex.Match(stringBuffer, 0, inputBuffer.Length);
    
            Console.WriteLine("Position:{0} Length:{1}.", match.Index, match.Length);
        }
    }
    

    Using this technique you can allocate a string “buffer” which can be re-used as the input to Regex, but you can mutate it with your bytes each time. This avoids the overhead of converting\encoding your byte array into a new .Net string each time you want to do a match. This could prove to be very significant as I have seen many an algorithm in .Net try to go at a million miles an hour only to be brought to its knees by string generation and the subsequent heap spamming and time spent in GC.

    Obviously this is unsafe code, but it is .Net.

    The results of the Regex will generate strings though, so you have an issue here. I’m not sure if there is a way of using Regex that will not generate new strings. You can certainly get at the match index and length information but the string generation violates your requirements for memory efficiency.

    Update

    Actually after disassembling Regex\Match\Group\Capture, it looks like it only generates the captured string when you access the Value property, so you may at least not be generating strings if you only access index and length properties. However, you will be generating all the supporting Regex objects.

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

Sidebar

Ask A Question

Stats

  • Questions 408k
  • Answers 408k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer >>> gen = (float(line.partition(' ')[0]) for line in open(fname)) >>>… May 15, 2026 at 6:51 am
  • Editorial Team
    Editorial Team added an answer I would recommend that you use a server side trace… May 15, 2026 at 6:51 am
  • Editorial Team
    Editorial Team added an answer I think Terminate All will kill all attached processes in… May 15, 2026 at 6:51 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.