question

RockHitman-2461 avatar image
0 Votes"
RockHitman-2461 asked TimonYang-MSFT commented

Regex output

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));

dotnet-csharpsql-server-transact-sqldotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TimonYang-MSFT avatar image
1 Vote"
TimonYang-MSFT answered TimonYang-MSFT commented
  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.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

for step 1, how did you obtained result B, |K, |L

Can you please in detail explain about this expression ?
string[] result = Regex.Matches(Invoice, **@"(?:^|\|)(""[^""]""|[^|])")


0 Votes 0 ·

@RockHitman-2461
Please check the link provided by TomPhillips and copy (?:^|\|)(""[^""]""|[^|]) there, the analysis there is very detailed.
Microsoft also provides some documents about Regex, which you can use as a reference.
Regular Expression Language - Quick Reference

0 Votes 0 ·

@RockHitman-2461
May I know whether your issue has been solved or not? If not, please share it in here. We can work together to figure it out.

0 Votes 0 ·
TomPhillips-1744 avatar image
0 Votes"
TomPhillips-1744 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.