开发者

Regex to Match a String Followed By a Another Word

So I am trying to write a regular expression that will match a defined string and then eventually another word. For example in the string SELECT * FROM persons ORDER BY name ASC LIMIT 10 I would like to match ORDER BY name ASC LIMIT. It seems like it should be s开发者_开发知识库imple but I haven't been able to figure it out.

Essentially I don't care whats between the ORDER BY and the LIMIT but I'd like to stop matching at the LIMIT. Here is what I have so far:

string pattern = @"\s*ORDER\s*BY.*LIMIT";

But it ends up matching the entire SELECT statement and I want to stop at the LIMIT.

Thanks in advance!


Try this modified pattern instead:

string pattern = @"\s*ORDER\s*BY.+?LIMIT";

Using .* is the likely culprit since it's a greedy match. To make it non-greedy add a ? to it, making it .*?. Personally I prefer to use .+? if I expect at least one character to be matched afterwards. For \s* you wouldn't care to make it non-greedy, but perhaps expect to use \s+ to ensure at least one whitespace character exists. In fact, you would want to use \b to match a word-boundary, rather than thinking in terms of whitespace, which would change the pattern to this:

string pattern = @"\bORDER\s+BY\b.+?\bLIMIT\b";

In other words, match the beginning of the word "ORDER" followed by at least one whitespace character, match "BY" and a word-boundary (the end of the word) then match any character at least once, non-greedy, till the complete word "LIMIT" is found. Knowing your data is important and you can probably get away with the first pattern. Word boundaries are useful to avoid partial matches and ensure a full word match. For example, see this related question.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜