Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
added 235 characters in body
Source Link
Anonguy123
  • 131
  • 1
  • 4

I am searching a string for a pattern that matches Material=. Some examples are Material=ABC123,Color=444555, Material=332212,Color=192929 I am new to C# and I learned about capture groups but I am wondering if its possible to do it in a cleaner way?

var pattern = new Regex("(?<label>Material)=(?<value>[^,]+)");
Match match = pattern.Match(Row2.Attributes);
var materialCode = (match.Success) ? match.Groups["value"].Value : "NA";

Edit:

I store materialCode into a database so it can't be null. If a value isn't found it has to be "NA". Also there are lots of combos which is why I specify I need whatever value after the = but before the , in the regex

I am searching a string for a pattern that matches Material=. Some examples are Material=ABC123,Color=444555, Material=332212,Color=192929 I am new to C# and I learned about capture groups but I am wondering if its possible to do it in a cleaner way?

var pattern = new Regex("(?<label>Material)=(?<value>[^,]+)");
Match match = pattern.Match(Row2.Attributes);
var materialCode = (match.Success) ? match.Groups["value"].Value : "NA";

I am searching a string for a pattern that matches Material=. Some examples are Material=ABC123,Color=444555, Material=332212,Color=192929 I am new to C# and I learned about capture groups but I am wondering if its possible to do it in a cleaner way?

var pattern = new Regex("(?<label>Material)=(?<value>[^,]+)");
Match match = pattern.Match(Row2.Attributes);
var materialCode = (match.Success) ? match.Groups["value"].Value : "NA";

Edit:

I store materialCode into a database so it can't be null. If a value isn't found it has to be "NA". Also there are lots of combos which is why I specify I need whatever value after the = but before the , in the regex

edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
Source Link
Anonguy123
  • 131
  • 1
  • 4

If a regex match is found assign it to a variable

I am searching a string for a pattern that matches Material=. Some examples are Material=ABC123,Color=444555, Material=332212,Color=192929 I am new to C# and I learned about capture groups but I am wondering if its possible to do it in a cleaner way?

var pattern = new Regex("(?<label>Material)=(?<value>[^,]+)");
Match match = pattern.Match(Row2.Attributes);
var materialCode = (match.Success) ? match.Groups["value"].Value : "NA";