开发者

C# Regular Expression

Can you explain me wha开发者_运维知识库t is the meaning of this regular expression. What would be the string which matches to this expression.

Regex(@"/Type\s*/Page[^s]"); 

what is @ symbol?? Thanks in advance.

Please provide full explaination. What would be the string which matches to this expression.


The @ symbol designates a verbatim string literal:

A verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character. A simple example is @"hello". In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the only exception being a quote-escape-sequence. In particular, simple escape sequences and hexadecimal and Unicode escape sequences are not processed in verbatim string literals. A verbatim string literal may span multiple lines.

As for the regular expression it breaks down like this:

/Type match this string exactly
\s* match any whitespace character zero or more times
/Page match this string exactly
[^s] match any character that isn't "s"


@ says that the string literal is verbatim.

The regex matches:

/Type followed by zero or more whitespaces, followed by /Page and a character that is not s

It will match strings like /Type/Pagex, /Type /Page3, /Type /Page?


@ starts a c# verbatim string, in which the compiler doesn't process escape sequences, making writing expressions with lots of \ characters easier.

both of the following match

/Type /Page4
/Type             /Pagex


Your regular expression matches any string containing the following:

  • A "/" character
  • The word "Type" (case sensitive)
  • Optionally, some whitespace
  • Another "/"
  • The word "Page" (case sensitive)
  • Any character that isn't an "s"

Examples would be "/Type /Paged" or "/Type/Pager".

If you want to match either "Page" or "Pages" at the end, you probably want this instead:

Regex(@"/Type\s*/Pages?");

Here is a good online C# regex tester.


Roughly, it matches: /Type{optional space}/Page{not an 's'}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜