2

I am trying to get the regex to match only when there is one letter from A-Z followed by a = like this A=, a=, B=, currently it is picking up any number of letters before the = like hem=, ac2=. Usually ^[a-zA-Z] works just fine but its not working for this case since I'm using named capture groups

String pattern = "FL2 (77) Flashing,77,a=1.875,A=90.0,b=3.625,B=95.0,c=1.375,C=175.0,d=2.5,hem=0.5,16GA-AL,";
var regex = new Regex("(?<label>[a-zA-Z]+)=(?<value>[^,]+)");

Other ways I've tried

var regex = new Regex("(?<label>^[a-zA-Z]+)=(?<value>[^,]+)");
var regex = new Regex("(?<label>[^a-zA-Z]+)=(?<value>[^,]+)");
Anonguy123
  • 407
  • 3
  • 16

6 Answers6

3

If you want to match l= but not word=, you need a negative look-behind assertion.

new Regex("(?<![a-zA-Z])(?<label>[a-zA-Z])=(?<value>[^,]+)")
ILMTitan
  • 10,751
  • 3
  • 30
  • 46
1

It's because you have a + after [a-zA-Z], which makes it match one or more characters in that character class. If you remove the +, it will only match one character before the =.

If you want it to only match in situations where there is exactly one alphabetical character before the equals sign, you will want to add to the beginning of the regex to make sure that the character before the letter you want to match is not a letter, like this:

(?<![a-zA-Z])(?<label>[a-zA-Z])=(?<value>[^,]+)

(notice though that this only matters in the case where you don't put a ^ before [a-zA-Z], in the case where you want matches that don't start at the beginning of a line)

Josh Withee
  • 9,922
  • 3
  • 44
  • 62
  • It doesn't match – Anonguy123 Jan 05 '18 at 17:31
  • @Anonguy123 are you saying it doesn't match anything you are trying to match? Or is it having the same problem you are having? Can you give some more details about what you are seeing? I just edited the regex, try it now. – Josh Withee Jan 05 '18 at 17:34
  • @Marathon55 Fair point, removed comment. Regex looks good to me. Your latest rendering is better than my suggestion. You hit on what I'd intended to type. – Trioj Jan 05 '18 at 17:35
  • @Anonguy123 try the regex I gave, it should work now. – Josh Withee Jan 05 '18 at 17:40
1

If the string pattern you have in your question is really the "haystack" in which you're looking for "needles", a really easy way to solve the problem would be to first split the string on ,, then use RegEx. Then you can use a simpler pattern ^(?<label>[a-zA-Z])=(?<value>.+)$ on each item in the list you get from splitting the string, and only keep the matches.

Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
0

Have you tried

var regex = new Regex("(?<label>^[a-zA-Z]?)=(?<value>[^,]+)");

I believe the "+" means 1 or more "?" means 0 or 1 or exactly 1 should be {1} (at least in python, not sure about C#)

var regex = new Regex("(?<label>^[a-zA-Z]{1})=(?<value>[^,]+)");
Teun D
  • 5,045
  • 1
  • 34
  • 44
A.B.
  • 58
  • 4
0

I recommend Regex.Matches over capture groups here:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string content = "FL2 (77) Flashing,77,a=1.875,A=90.0,b=3.625,B=95.0,c=1.375,C=175.0,d=2.5,hem=0.5,16GA-AL,";
            const string regexPattern = "(?<=[,| ])[a-zA-Z]=([0-9|.|-])+";

            string singleMatch = new Regex(regexPattern).Match(content).ToString();
            Console.WriteLine(singleMatch); // a=1.875

            MatchCollection matchList = Regex.Matches(content, regexPattern);
            var matches = matchList.Cast<Match>().Select(match => match.Value).ToList();
            Console.WriteLine(string.Join(", ", matches)); // a=1.875, A=90.0, b=3.625, B=95.0, c=1.375, C=175.0, d=2.5
        }
    }
}
C. McCoy IV
  • 887
  • 7
  • 14
0

Assuming that the label is separated by a comma (which seems to be the case based on your example and code) then you can use:

^|,(?<label>[A-Za-z])=(?<value>[^,]+)
aquinas
  • 23,318
  • 5
  • 58
  • 81