0

I have some very "messy" strings that come in like this:

  • CR 722-2018
  • CR7222018
  • -CR 7222018

I need them converted into something like this:

CP-41-CR-0000722-2018

All three of the above would wind up looking like this.

I was curious if there was a way to accomplish this with RegEx? The format would look something like:

CP-41-CR-(\n{7})-(\n{4}).

Is this something that can be done with RegEx?

4
  • 3
    You dont really need Regex, you just need to strip out all non-digit characters and then interpolate the string Commented Feb 26, 2020 at 14:20
  • 1
    To expand on Maccuttura comment "T0e1s2t3S4t5r6i7n8g9h10e11r12e13".Where(Char.IsDigit).Take(7) A simple .Where(Char.IsDigit).Take(7) should do the job Is the CR important does it vary? Do you need to pad with 0 the 722 part or does it range from 001-999? Some test case to cover all edge case would be nice.. Commented Feb 26, 2020 at 14:22
  • For the year par do you want to enforce the 4 digit or "CR 72220-18" will give a valid "CP-41-CR-0072220-18"? Commented Feb 26, 2020 at 14:28
  • @xdtTransform, I would want to enforce 4 digit -- default to 20xx Commented Feb 26, 2020 at 14:43

2 Answers 2

2

Yes, that can be done with RegEx as follows:

var patt = @".*?(CR)\s?(\d\d\d)\-?(\d\d\d\d)";

Regex.Matches(txtIn.Text, patt, RegexOptions.Multiline | RegexOptions.IgnoreCase).Cast<Match>()
    .ToList().ForEach(m => 
    Console.WriteLine($"CP-41-{m.Groups[1].Value}0000{m.Groups[2].Value}-{m.Groups[3].Value}"));

Where the txtIn.Text is the input of the messy strings.

Here is a test.

Also the pattern .*?(CR)\s?(\d{3})\-?(\d{4}) will do.

Sign up to request clarification or add additional context in comments.

2 Comments

May be 0000{m.Groups[2].Value} can be written as {m.Groups[2].Value.PadLeft(7,'0')}
@MKR uhh. Only the big boys can code like that. Thank you mate.
1

Try following :

            string[] inputs = {
                                  "CR 722-2018",
                                  "CR7222018",
                                  "-CR 7222018"
                              };
            foreach (string input in inputs)
            {
                string digits = string.Join("", input.Where(x => char.IsDigit(x)));
                string output = string.Format("CP-41-CR-000{0}-{1}", digits.Substring(0, 3), digits.Substring(3));
                Console.WriteLine(output);
            }
            Console.ReadLine();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.