I can't figure out what happens to the "FROM ROOT" when the Regex.Replace() happens:
static string query = @"
SELECT *
FROM root
ORDER BY root['UtcTimestamp'] DESC";
static void Main(string[] args)
{
var endsWithOrderBy = new Regex("^.+(\\s+ORDER\\s+BY\\s+.+)$",
RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
query = query.Trim();
var result = endsWithOrderBy.Replace(query, "$1");
Console.Write(result);
Console.Read();
}
I think it's because there's a \n instead of a space between "FROM root\nORDER BY". How can I tell the regex to include \n characters in \\s?
NOTE: Please refrain from discussions about the applicability of using regular expressions here, given that the example here is contrived.
\smatches all kinds of whitespace characters, including\n. So that's definitely not your problem, but I don't get what you're trying to do. If you want the regex to match the whole string, add theSinglelineoption; if you want to match just last line, remove the^.+and the first\s+. And while you're at it, use verbatim string literals (@"...") so you don't have to escape the backslashes, and drop theCompiledoption: you don't need it.