1

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.

2
  • What is your expected behavior here? What are you trying to do? Commented Mar 16, 2016 at 18:15
  • \s matches 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 the Singleline option; 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 the Compiled option: you don't need it. Commented Mar 16, 2016 at 20:34

2 Answers 2

2

Looking at the documentation, since you've specified RegexOptions.Multiline, the ^ anchor matches the beginning of a line, not the beginning of the string.

As a result, your pattern matches:

FROM root
ORDER BY root['UtcTimestamp'] DESC

You then replace that with the value of the first capture, which is:

{\n}
ORDER BY root['UtcTimestamp'] DESC

which gives the result:

SELECT *

ORDER BY root['UtcTimestamp'] DESC

It's not clear what output you were expecting. If you just want to remove the entire ORDER BY clause, then add a group to capture the prefix:

var endsWithOrderBy = new Regex("^(.+)(\\s+ORDER\\s+BY\\s+.+)$", ...);
Sign up to request clarification or add additional context in comments.

Comments

1

You can use this:

\s*\n?\s*(ORDER\s+BY\s+.*)$

If you use an anchor for the start of the line (^) you cannot match when the ORDER BY is not preceded by a line break, i.e. SELECT * FROM root ORDER BY root['UtcTimestamp'] DESC.

Check here: https://regex101.com/r/gQ2rQ1/2

I'm using the g modifier so you can test multiple sentences, but you won't need it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.