.net Regex sharing a sequence of matches

Knowing Regex does not allow me, and I need help.

I am trying to take string input that might look like this

@"RRR:domsd\ddf:sdf:dsf:f"

or

@"sds4:kwertss\wegggds:rowdf"

and in both cases, separate everything from the beginning and after \, but before the first :that appears after firt \.

Thus, the results will be as follows:

RRR:domsd\ddf
sds4:kwertss\wegggds

if there was no match, if no sequence was found : \ :.

I have tried the following two registers so far

1st: @".*[:].*[\\].*[:]"which matches all last :, not first after\

2nd: @"(.+?:.+?):(.+)"which just matches the whole line

+4
source share
3

, :

^[^:]*:[^\\]*\\[^:]*(?=:)

regex

  • ^ -
  • [^:]* - 0+, :
  • : -
  • [^\\]* - 0+, \
  • \\ - \
  • [^:]* - 0+, :
  • (?=:) - , :.

#:

var m = Regex.Match(s, @"^[^:]*:[^\\]*\\[^:]*(?=:)");
var res = string.Empty;
if (m.Success) 
{
     res = m.Value;
}
+3

( )

(^.*\\.*?):

.

0

: ^(.*:.*[\\][^:]*):. , : \ :. .

.

0

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


All Articles