0

I have the following regex code:

@"(N[0-9][EHPULMAVRYGBWK123670]{4}[N]{1}PF[0]{1}[0-9]{1})";

The [EHPULMAVRYGBWK123670] within the regex refer to specific button types or colours.

There are four buttons in total, and the order they are in the part number denotes the order that they are in the product (from top left to top right).

For example if the part number contained:

RGBY - Red (Top Left), Green (Top Right), Blue (Bottom Left), Yellow (Bottom Right)

GBYR - Green (Top Left), Blue (Top Right), Yellow (Bottom Left), Red (Bottom Right)

After the buttons, there is always the letter N, and a PF number.

What I want to do is extract the 4 letter combination for the colors. The {4} in the regex is what captures those letters. I then needs to make a decision based on the order of the letters.

How would I go about doing this?

11
  • What is this save this regex in a string which you are talking about? Commented Jun 4, 2018 at 14:12
  • 2
    Can you give an example of your input? Maybe regex is a bit overkill for this scenario Commented Jun 4, 2018 at 14:14
  • 1
    I think I understand what he is doing. The regex is necessary to extract the 4 letter combination for the colors. The {4} in the regex is, I believe, what captures those letters. He then needs to make a decision based on the order of the letters. Commented Jun 4, 2018 at 14:15
  • 1
    @JuanR Yes this is precisely what I want to do, sorry I'm unable to phrase it correctly. Commented Jun 4, 2018 at 14:16
  • 2
    A small detail but [N]{1} is also just N Commented Jun 4, 2018 at 14:29

1 Answer 1

3

You need to modify your regex slightly so that it captures the four letters in question. You can then decide what to do with them:

var pattern = @"(N[0-9]([EHPULMAVRYGBWK123670]{4})[N]{1}PF[0]{1}[0-9]{1})";
var test = "NO4A6SRP11N2UBWYNPF05";
var regex = new Regex(pattern, RegexOptions.IgnoreCase);
var result = regex.Match(test);
if(result.Success)
{
    var value = result.Groups[2].Value;
    switch (value)
    {
        case "UBWY":
            //Do something
            break;
        case "RBYG":
            //Do something
            break;
        default:
            break;
    }
}

Notice the parenthesis around the pattern that matches the four letters.

There are more elegant approaches to deciding what to do with the four letter code. In this case I have provided a simple switch statement for illustration purposes.

Alternatively, you can examine the string you capture letter by letter:

//Character by character, in order
for (int i = 0; i < value.Length; i++)
{
    char letter = value[i];
    //Decide what to do here. 
}


//Or check positions by index
if(value[0] == 'U')
{
    //Decide what to do here.
}

Depending on how many combinations are possible, you might want to consider using a state machine.

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

6 Comments

Wouldn't it be more logical to split the 4 characters? You'll get an awful lot of permutations (cases) this way.
@Stefan: Correct. That is why at the bottom of my answer I mentioned that he might want to use a state machine because of that. :-)
This is brilliant, thank you! Am I able to do the cases individually based on the position however rather than as a group? There are far too many variations otherwise.
When I earlier said that I wanted to save the results of the Regex into a string, it was so that I could use the position of a character within a string and then use a case statement based on that. For example looking at character at position [1] - if B -> do something, if C -> do something. How would I go about doing that?
One other suggestion is naming the capture group you are interested in. For example: (?<color>[EHPULMAVRYGBWK123670]{4}) I find this much easier to handle than using the index which is hard to read and is prone to getting messed up if you decide you need to add more groups. You can retrieve a named group with result.Groups["color"].Value
|

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.