Royson
Royson

Reputation: 2901

C# Regular Expression

Can you explain me what 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.

Upvotes: 1

Views: 1726

Answers (5)

Chris Fulstow
Chris Fulstow

Reputation: 41872

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

Upvotes: 1

Richard Beier
Richard Beier

Reputation: 1732

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.

Upvotes: 1

µBio
µBio

Reputation: 10748

@ 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

Upvotes: 1

Amarghosh
Amarghosh

Reputation: 59451

@ 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?

Upvotes: 2

Andrew Hare
Andrew Hare

Reputation: 351476

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"

Upvotes: 6

Related Questions