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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:56:57+00:00 2026-06-15T22:56:57+00:00

I’m using a regular expression replace (in a .Net application) to create an item

  • 0

I’m using a regular expression replace (in a .Net application) to create an item id from a barcode. Here’s the regex I’m using:

^(?<Bestellnr>\w{6})(?<Pos>\d{3})M(?<Menge>\d{5})P(?<Lfd>\d{3})$

The replacement string is (it has leading blanks, but that’s not important here):

${Bestellnr}${Pos} ${Lfd}

An example input would be:

685774010M00555P002

The current result of the above replacement is:

685774010 002

But now my customer wants me to remove the leading zeroes from the groups “Pos” and “Lfd”, so the result should change to:

685774 10   2

Edit: Please note that the two zeroes in the “lfd” group are replaced by two blanks!

I’ve tried for hours now, but I cannot seem to get this working. Best approach I was able to find was to create extra groups for the leading zeroes like:

^(?<Bestellnr>\w{6})(?<PosNull>0*)(?<Pos>\d{1,})M(?<Menge>\d{5})P(?<LfdNull>0*)(?<Lfd>\d{1,})$

But I don’t know how I can replace the “null groups” with the correct number of blanks. Is this possible at all? (Until now I thought there’s nothing that’s not possible with Regex 😉

Can anyone help?

  • 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-15T22:56:58+00:00Added an answer on June 15, 2026 at 10:56 pm

    Ok, found a solution that fits my needs. It’s done with a simple String.Format, passing it the Groups of the Regex converted to ints. Groups are accessed by index, but since it’s done in the format string, no hardcoding is required.

    Here’s the code:

    // both from app.config
    Regex regex = new Regex(@"(?<Bestellnr>\w{6})(?<Pos>\d{3})M(?<Menge>\d{5})P(?<Lfd>\d{3})");
    string format = "   {0,6:d}{1,3:d} {3,3:d}";    
    
    string barcode = "685774010M00555P002";
    
    Match match = regex.Match(barcode);
    if (match.Success)
    {
        var groups = (from Group grp in match.Groups select grp.Value).Skip(1).Select(x => (object)int.Parse(x)).ToArray();
        string s = string.Format(format, groups);
        Console.WriteLine(s);
    }
    

    Remarks:

    • Skip(1) is necessary because the first group in the collection is the complete match itself.
    • the cast to object is required because string.format needs a params object[] array and won’t work with int[]
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I

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.