-2

I am running the following regular expression

Regex.Match(x, "<([^\s]+)")

but \s shows an error "Unrecogonised escape sequence" If I do this

Regex.Match(x, @"<([^\s]+)")

It still wont work.

I want to find a sequence of character that starts with < then any character until a space is found or > is found.

Edit:

I am editing this incase someone finds it and wants to know the solution. This was back when I was still learning Regex and no one had told me about escaping my characters. All that was needed was a double backslash to escape the special character.

chillinOutMaxin
  • 182
  • 1
  • 13

3 Answers3

1

You need to provide escaping sequence instead of using Regex.Match(x, @"<([^\s]+)") [Single slash] use [Double Slash] to avoid an error "Unrecogonised escape sequence" Regex.Match(x, @"<([^\\s]+)");

May be this helps. Keep posted if you need more info about it.

1

need to provide escaping sequence @"<([^\s]+)"

string pattern = @"<([^\s]+)";
        string input = @"<sada >sffds</sada>";

        foreach (Match m in Regex.Matches(input, pattern))
        {
            Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
        }
santosh singh
  • 27,666
  • 26
  • 83
  • 129
0

It is true that you must use a @"" string (or escape the backslashes themselves by doubling them \\, but it is not easy to read this way).

The backslashes have very special meaning in strings (escape sequences for special unprintable characters, before any regular expression interpretation). So, you must tell the C# compiler not to interpret the backspaces as escape characters.

This regex should do the trick : <([^\s>]+)

(match a < then capture everything that is not a space or a >)

RegexStorm link

In the string :

abcdef <ghij klm nop qrs tu<vwxy>z

ghij and vwxy are the 2 captured groups.

Pac0
  • 21,465
  • 8
  • 65
  • 74