Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

337
Views
Capture group on (possibly empty) string?

If I have a string and wish to capture a possibly empty string (and subsequently check if indeed it was empty) how can I do this? My regular expression works when the captured section is not empty and on first impressions it might seem to work in case the string is empty since Console.WriteLine(second); produces no output.

string notEmpty = "abc|CaptureMeButICanBeEmpty|def";
string isEmpty = "abc||def";
string regEx = @"^\w+\|([^|]+|)\|\w+$";
var match = Regex.Match(notEmpty, regEx);
var match2 = Regex.Match(isEmpty, regEx);

string first = match.Groups[1].Value;
Console.WriteLine(first); // CaptureMeButICanBeEmpty
string second = match2.Groups[1].Value;
Console.WriteLine(second); // No Output

Console.WriteLine(match.Groups.Count); // 2
Console.WriteLine(match2.Groups.Count); // 2

However, it is impossible to know if second is empty because the capture group was empty or simply because there was no second capture group at all. That is to say:

string third = match2.Groups[2].Value;
Console.WriteLine(second); // No Output

also produces no output, since the capture group is non-existent, and testing with something like

Console.WriteLine(string.IsNullOrEmpty(second));
Console.WriteLine(string.IsNullOrEmpty(third))

does not work either as both return true (I expected perhaps string.IsNullOrEmpty(second) to return true but string.IsNullOrEmpty(third) to throw an index out of bounds exception or something similar.

I realise I can use the result of match.Groups.Count to determine how many capture groups there should be in the sample code [note: C# defines Groups[0] as the entire match] but I do not want to have to rely on this.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Your attempt to check group count is wrong because the number of groups is set by (or in) the regex pattern: the number of groups is always the amount of capturing groups + 1 (for the whole match). So, if you check match.Groups.Count it will always give you 2 because ^\w+\|([^|]+|)\|\w+$ contains a single capturing group.

Now, it is not possible to use an optional pattern inside an obligaotry capturing group to check if a group matched or not, because the group value will be initialized with an empty string regardless, and the group .Success property will then be set to true.

The only way out here is to make the capturing group pattern optional and then check the match group Success property, and if it is true, get the string value of the group, else, assign the default value of your choice.

Here is a C# demo:

string notEmpty = "abc|CaptureMeButICanBeEmpty|def";
string isEmpty = "abc||def";
string regEx = @"^\w+\|([^|]+)?\|\w+$";
var match = Regex.Match(notEmpty, regEx);
var match2 = Regex.Match(isEmpty, regEx);
 
var first = match.Groups[1].Success ? match.Groups[1].Value : "No ouptut";
Console.WriteLine(first); // => CaptureMeButICanBeEmpty
var second = match2.Groups[1].Success ? match2.Groups[1].Value : "No ouptut";
Console.WriteLine(second.ToString()); // => No Output

Well, you may actually perform any actions you need if you use an if block after checking .Success property.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!