使用正则表达式进行搜索,就像在SQL中一样 [英] Searching using Regular expressions as in SQL Like

查看:98
本文介绍了使用正则表达式进行搜索,就像在SQL中一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我花了很长时间弄清楚像在SQL中一样用C#在正则表达式中进行搜索.我在C#中找不到SQL的等效%"符号.
我尝试使用(|)",但它给出了或"条件.
我想知道我是否可以在正则表达式上使用带有和"条件的分组.


例如,

如果我在tblTest中有一条记录,其strName字段为;

ClosedAccountFile_01_13_2012_565665.txt

如果我想在SQL数据库中进行搜索,我会去;

从tblTest中选择*,其中strName类似于''%ClosedAccountFile [_]%[_]%[_]%[_]%.txt%"

这将是SQL中的一个匹配项.

百分号使我可以选择按给定顺序在另一个字符串中搜索多个字符块.

如何替换上面文本中的%符号,以便可以在C#正则表达式中进行相同的搜索?

感谢您的帮助.

Hi,
I spent long time to figure out making the search with regular expressions in C# as it is done in SQL. I cannot find the equivalent of ''%'' sign of SQL in C#.
I have tried to use "(|)" but it gives the ''or'' condition.
I am wondering if I can use the grouping with "and" condition on Regular Expressions.


For example ,

If I have a record in the tblTest whose strName field is;

ClosedAccountFile_01_13_2012_565665.txt

If I like to make a search in SQL database, I would go;

select * from tblTest where strName like ''%ClosedAccountFile[_]%[_]%[_]%[_]%.txt%"

And that would be a match in SQL.

The Percent Sign gives me the option to search multiple character blocks in the given order within another string.

What can I replace the % sign on the text above so that I can make the same search in C# Regex?

Your help is appreciated.

推荐答案

在这种情况下,请尝试:
In this case, try:
ClosedAccountFile_(\d+)_(\d+)_(\d+)_(\d+).txt

.这与您的示例相符,并将每个数字值都分离出来.在比赛中属于自己的组.

您已经认识到Regexes比SQL LIKE语句复杂得多,在Regext中最简单的%"形式是.*",它可以匹配任意数量的任何字符:通常有更好的方法可以在正则表达式.

获取 Expresso [ ^ ]-它是免费的,它可以检查并生成正则表达式.

This matches your example, and separates each of the numeric values out into it''s own group within the match.

You have top realize that Regexes are much, much more complex than SQL LIKE statements - the simplest form of "%" in a Regext is ".*" which matches any number of any character: there are normally better ways to do this in a Regex.

Get a copy of Expresso [^] - it''s free, and it examines and generates Regular expressions.


(.*)似乎可以正常工作%在SQL中有效.
感谢您的回答.
(.*) seems like working as % works in SQL.
Thanks for the answer.


这篇关于使用正则表达式进行搜索,就像在SQL中一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆