-2

I have been trying to figure out regex for two days now and I can't seem to get it to work. I'm trying to setup regex that'll find all of the part numbers for a specific part family, i.e. if I know all of the part numbers for this family use the following format:

part number schema

How would you specify: "The first character should be a 'C', followed by one of the following 4-character combinations, then another C, then any 3-digit number between 000 and 999 (has to be 3 characters, even if all zeros), then J, K, or M, then one of these characters or numbers" etc, etc.

Andrew Morton
  • 21,016
  • 8
  • 48
  • 69
Tom
  • 1
  • 2
  • Sorry Tom, could you provide some code pls? And in which language are you programming O_o ? –  Aug 21 '20 at 18:25
  • @LuckyFr I'm looking for it to language agnostic, just the regex patterns. This may be implemented in excel short term, and long term in a confluence/jira macro. – Tom Aug 21 '20 at 18:29
  • 1
    @Tom It might help you to play around with one of the online regex explainers, for example [regex101.com](https://regex101.com/r/DleTd9/1) - that link should show you `^C(0402|0603|0805)C[0-9]{3}(J|K|M)(9|8|4|3|5|1|2|A)R?A?C?`. (*You* can type all the package sizes ;) ) – Andrew Morton Aug 21 '20 at 18:32
  • 1
    @AndrewMorton Wow, that tool is super useful for debugging. Thank you! – Tom Aug 21 '20 at 18:51

2 Answers2

0

I don't know, which language you use, but in C#, you can try this expression:

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string pattern = @"C[0-9]{4}C[0-9]{3}[JKM]{1}";
        string input = @"C4550C455J";
        RegexOptions options = RegexOptions.Singleline;
        
        Match m = Regex.Match(input, pattern, options);
        Console.WriteLine("'{0}' found at index {1}", m.Value, m.Index);
    }
}
Nikolay B.
  • 21
  • 6
0

The pattern would be

C(0402|0603|0805|1206|1210|1808|1812|1825|2220)C([0-9]{3})([JKM])([1234589A])RAC

sebastian
  • 432
  • 1
  • 4
  • 14