Why does this regular expression work in JavaScript, but not C #?

Expression

var regex = new Regex(@"{([A-z]*)(([^]|:)((\\:)|[^:])*?)(([^]|:)((\\:)|[^:])*?)}");

Structure

The expression [roughly] is intended to search for tokens inside the input using the format:, {name[:pattern[:format]]}where patternand formatare optional.

{
  ([A-z]*) // name
  (([^]|:)((\\:)|[^:])*?) // regex pattern
  (([^]|:)((\\:)|[^:])*?) // format
}

In addition, the expression attempts to ignore the escaped colon, which allows the use of strings such as {Time:\d+\:\d+\:\d+:hh\:mm\:ss}

Question

When testing RegExr.com everything works enough, however, when you try to use the same template in C #, the input does not work, why

(Any recommendations for general expression enhancements are also very welcome)

+4
source share
1 answer

[^] JavaScript, , ( ES5 - BMP). # char . RegexOptions.Singleline. JS , char [\s\S].

, , , ([^]|:) [\s\S], : ( [\s\S] ).

, [A-z] ASCII. [a-zA-Z], [A-z] .

,

{([A-Za-z]*)([\s\S]((\\:)|[^:])*?)([\s\S]((\\:)|[^:])*?)}

. .NET regex test JS regex.

, : , ( ) .., .

+6

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


All Articles