Search code examples
.netregexnintex-workflow

Reg Ex to extract 2 texts from a variable


I need 2 separate regex (.net, c#) to extract values. I can't use grouping. Just need 2 separate Regex. I tried this (?:^|(?:[.!?]\s))(\w+) but it gives me 6;. Supplier

For example

6. Supplier - Compressors, Drivers & Refrig. Units

1st regex will give me Supplier 2nd regex will give me Compressors, Drivers & Refrig. Units

example variables are (they are not together like this in my scenario). They are separate instance. The reason I need to separate Regex is to evaluate this value at different position in the code.

4. Cost/Estimating
6. Supplier - Minor Material: Specialty
6. Supplier - Pressure Vessels & Filters
6. Supplier - Pumps
6. Supplier - Minor Material: Valves
6. Supplier - Minor Material: Specialty
6. Supplier - Other Major Equipment
7. Manufacturing
8. Project Management
9. Order Release (OTR) - Commercial Documents
9. Order Release (OTR) - Hand-Off

Solution

  • You can use one regex with grouping:

        string myString = "6. Supplier - Minor Material: Specialty";
        var regexPattern = @"(?<number>\d+)\.\s(?<supplier>\w+)\s-\s(?<product>.+)";
        var matched = Regex.Match(myString, regexPattern);
        if (matched.Success)
        {
            var supplier = matched.Groups["supplier"].Value;
            var product = matched.Groups["product"].Value;
        }
    

    Based on your requirements for Nintex, you could try the following two regular expressions, though they're far from ideal:

    Matches first word after number (with lookahead and lookbehind):

    (?<=\d\.\s).+(?=\s-\s)
    

    Matches words after dash - (with lookbehind)

    (?<=\s-\s).+