Click here to Skip to main content
15,921,837 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
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.
Posted

In this case, try:
ClosedAccountFile_(\d+)_(\d+)_(\d+)_(\d+).txt
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.
 
Share this answer
 
Comments
Michel [mjbohn] 14-Jan-12 7:35am    
Assuming that the file name format is clear-cut I'd use /ClosedAccountFile_(\d{2})_(\d{2})_(\d{4})_(\d{6}).txt/ to also check for right format.
But anyway, my 5 :)
OriginalGriff 14-Jan-12 7:45am    
Yep - so would I. But from one sample... :laugh:
Member 8566049 14-Jan-12 19:26pm    
Thanks for the comments. Unfortunately the file name format is very variant.
That is why I had to convert any date type to "%". So that I could search in Sql.
\d converts the digits buy I was looking for some expression to replace the "%" sign only. Again, thanks.
(.*) seems like working as % works in SQL.
Thanks for the answer.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900