Is there a problem with my regex?

I am trying to find the number of matches in a relative path for reference links (" ..\"). So I have the following template: " (\.\.\\)", which works as expected for the path " ..\..\a\b", where it will give me two successful groups (" ..\"), but when I try the path " ..\a\b" it will also return 2 when it will return 1. I tried this in a reg ex tool like Expresso and it seems to work as expected, but not in .net, any ideas?

+3
source share
4 answers

I get the correct answer, try the following:

Console.WriteLine(Regex.Matches(@"..\..\a\b", @"(\.\.\\)").Count); //2
Console.WriteLine(Regex.Matches(@"..\a\b", @"(\.\.\\)").Count); //1

\ .NET?

0

:

(\.\.\\)

(.) , . , .

+1

Expresso .net, " reg ex, Expresso, , , , .net", , .
, , , . Regex, , , .

Hope this helps!

0
source

Did you run back to avoid points in the Regex? That is, "\\.\\.\\\\"or @"\.\.\\"?

You can always not use Regex for this task and use

Int32 count = url.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries)
                 .Where(s => s == "..")
                 .Count();

instead of this. =)

0
source

Source: https://habr.com/ru/post/1737539/


All Articles