Regex output

Rock Hitman 46 Reputation points
2021-09-16T05:56:49.39+00:00

Hi, I have this below Regex expression, just like to know what each step is doing below and the output ?

string Invoice = BSC|KS1|LS1
string[] result = Regex.Matches(Invoice, @"(?:^|\|)(""[^""]""|[^|])")
.Cast<Match>()
.Select(m => m.Groups[1].Value)
.ToArray();
return result.Any(s => InvoiceID.Contains(s));

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,525 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,637 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,584 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Timon Yang-MSFT 9,576 Reputation points
    2021-09-16T08:16:54.523+00:00
    1. string[] result = Regex.Matches(Invoice, @"(?:^|\|)(""[^""]""|[^|])") Find one or more results that match the regular expression in Invoice. In this example, the result obtained is B, |K, |L
    2. Case<Match> Convert it into a Match collection, which is convenient for further processing with linq in the subsequent code.
    3. Select(m => m.Groups[1].Value) To get the first capture group in the regular expression, you can check the following post to understand this. Regular Expression Groups in C# The result of this step is B, K, L.
    4. return result.Any(s => InvoiceID.Contains(s)) InvoiceID is a variable, I think it should be the parameter of this method. Check whether the InvoiceID contains one of the results of the third step. Like B, BS, and KA all return True.

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  2. Tom Phillips 17,716 Reputation points
    2021-09-16T12:29:50.877+00:00

    You can plug your string into https://regexr.com/ and it will tell you exactly what it is doing.

    0 comments No comments