Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
How do i meet this criteria:

Match

AAA123.xlsx
BBB123.xlsx

Don't Match
~$AAA123.xlsx



The below seems to work, but how can i check for the ~ ?

[^\$](AAA|BBB)123.*.xlsx
Posted
Comments
Tomas Takac 17-Oct-14 9:54am    
I would do this to match your strings (and not match anything else): ^(AAA|BBB)123.*\.xlsx$
But it seems to me that you are looking for something else here.
BillWoodruff 17-Oct-14 9:56am    
Do ~ or $ appear only at the first character of the string ?

Use the start-of-string and end-of-string indicators:
^(AAA|BBB)123.*\.xlsx$
Note the backslash before the final dot '.' character - if you don't have that then it matches any character, so it would match AAA123myxslx.txt as well.

Get a copy of Expresso [^] - it's free, and it examines and generates Regular expressions.
 
Share this answer
 
Comments
Nicholas Marty 17-Oct-14 9:57am    
I like http://regexpal.com/ to test my regex ;)
OriginalGriff 17-Oct-14 10:08am    
Not so fond of it myself: Expresso explains them in English, helps you write them and will produce VB / C# code ready for insert into your app, which is handy. I did try the VS extension "Regex Editor" which did much what regexpal does, but within VS - but I still went back to Expresso! :laugh:
Nicholas Marty 17-Oct-14 10:39am    
Well, I don't regularly try to express something with regular expressions (pun intended ;-)). And I my knowledge for writing the simple ones I need is enough for not needing a tool to construct them. I only need a tool to get some basic verification if the expression is actually doing what I need. As soon as I've figured it out I usually write a small sample script in LinqPad to be sure .Net handles the expression as I expect it to, before finally writing the unit tests to it. Sure TDD would suggest to write the Tests first. But I think it's a bit tedious to test a regex using unit tests ;)
OriginalGriff 17-Oct-14 10:47am    
:laugh:
I'd have to agree - I wouldn't want to do that either!
They are a handy tool, but by heck they are easy to get wrong!
Thanks for your help!

I tried the Expresso tool - nice.
however i see some strange issues..
--------------------------------------------------------------
[^\$](AAA|BBB)123.*.xlsx
Is working in my C# program

Match
AAA123.xlsx
BBB123.xlsx

Don't Match
~$AAA123.xlsx

But in the Expresso tool it does not match any of them.
--------------------------------------------------------------
Then I tried your suggestion:

^AAA|BBB)123.*.xlsx$

That is working perfectly in the Expresso tool, but when I use the Beginning of text ^ marker in my C# program, it matches nothing at all !

That statement was wrong, my own fault ! It's working as it should
--------------------------------------------------------------

Sometimes there are corrupt xlsx files in my search folders (prefixed with ~$), that is why i need this, because i don't want to handle those files.


P.S.

In Expresso the 2 first lines below are matching ((AAA|BBB)123.*.xlsx), and the last one only partially match. But in my C# program all three match. Is there some flag i should set in C# not to "match" something only "partially mathcing" ?

VB
AAA123.xlsx
BBB123.xlsx
~$AAA123.xlsx
 
Share this answer
 
v5

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