I have a pattern to match with the string: string pattern = @"asc" I am checking the SQL SELECT query for right syntax, semantics, ... I need to say that in the end of the query string I can have "asc" or "desc". How can it be written in C#?
我有一个匹配字符串的模式:string pattern = @“asc”我正在检查SQL SELECT查询的正确语法,语义,...我需要说在查询字符串的末尾我可以有“asc “或”desc“。如何用C#编写?
4 个解决方案
#1
4
That'd look like
那看起来像
new Regex("asc$|desc$").IsMatch(yourQuery)
assuming that you're mandating that asc/desc is the end of the query. You could also do (?:asc|desc)$
which is a little cleaner with the single $
, but less readable.
假设您要求asc / desc是查询的结尾。你也可以做(?:asc | desc)$,这对于单个$有点干净,但可读性较差。
#2
1
You want the "alternation" operator, which is the pipe.
你想要“交替”运算符,这是管道。
For instance, I believe this would be represented as:
例如,我相信这将表示为:
asc|desc
If you want to make it a non-capturing group you can do it in the normal way. I'm not a regex expert, but that's the first thing to try.
如果您想使其成为非捕获组,您可以以正常方式执行此操作。我不是正则表达式专家,但这是第一个尝试的东西。
EDIT: MSDN has a page about regular expression language elements which may help you.
编辑:MSDN有一个关于正则表达式语言元素的页面,可以帮助您。
#3
1
Just to add to the other answers, you probably wanna apply an ignore case option to the regex as well.
只是为了添加其他答案,您可能还想将一个忽略大小写选项应用于正则表达式。
string tempString = "SELECT * FROM MyTable ORDER BY column DESC";
Regex r = new Regex("asc$|desc$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
bool answer = r.IsMatch(tempString);
#4
0
I reckon something like the following works as a regex in .Net:
我认为以下作为.Net中的正则表达式:
asc|desc\z
.. it will match end of line as well as end of string. If you just want end of string try:
..它将匹配行尾和字符串结尾。如果你只想结束字符串试试:
asc|desc$
#1
4
That'd look like
那看起来像
new Regex("asc$|desc$").IsMatch(yourQuery)
assuming that you're mandating that asc/desc is the end of the query. You could also do (?:asc|desc)$
which is a little cleaner with the single $
, but less readable.
假设您要求asc / desc是查询的结尾。你也可以做(?:asc | desc)$,这对于单个$有点干净,但可读性较差。
#2
1
You want the "alternation" operator, which is the pipe.
你想要“交替”运算符,这是管道。
For instance, I believe this would be represented as:
例如,我相信这将表示为:
asc|desc
If you want to make it a non-capturing group you can do it in the normal way. I'm not a regex expert, but that's the first thing to try.
如果您想使其成为非捕获组,您可以以正常方式执行此操作。我不是正则表达式专家,但这是第一个尝试的东西。
EDIT: MSDN has a page about regular expression language elements which may help you.
编辑:MSDN有一个关于正则表达式语言元素的页面,可以帮助您。
#3
1
Just to add to the other answers, you probably wanna apply an ignore case option to the regex as well.
只是为了添加其他答案,您可能还想将一个忽略大小写选项应用于正则表达式。
string tempString = "SELECT * FROM MyTable ORDER BY column DESC";
Regex r = new Regex("asc$|desc$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
bool answer = r.IsMatch(tempString);
#4
0
I reckon something like the following works as a regex in .Net:
我认为以下作为.Net中的正则表达式:
asc|desc\z
.. it will match end of line as well as end of string. If you just want end of string try:
..它将匹配行尾和字符串结尾。如果你只想结束字符串试试:
asc|desc$