0

I am fairly new to using regex and I am not entirely clear on the syntax for everything yet. Basically I am stuck on a bit of code where I have:

if(@"\d{2,}\s"+string == Path.GetFileNameWithoutExtension(dir)) 
{ 
    do stuff 
} 

My problem is that it won't match anything. I basically have a bunch of files that it's searching through that all have 2 digits and a space, then the name that the user is searching for. Can I combine regex + string like that or is the problem with my regex/statement? Just for clarity, it will match when I actually remove the two digits and space from the files. I apologize if the problem is obvious, I've only been playing with regex for a few days...

4
  • That's definitely not correct C# syntax. Commented Mar 28, 2014 at 22:47
  • String == doesn't have anything to do with regexes. It will just do a literal text compare. You need to use Regex.IsMatch or Regex.Matches to compare a string with a regex, not == Commented Mar 28, 2014 at 22:48
  • 1
    No. If that were valid syntactically (which it isn't), that does nothing to evaluate the regular expression. It would simply concatenate \d{2,}\s and the content of string into a single string - what you call a "regex" would be simply a literal string. Instead of trying to hack a regex into things (which isn't always appropriate), why not explain what you're trying to accomplish and ask how to to so? Commented Mar 28, 2014 at 22:49
  • If you want a string to be considered as a part of a regular expression, strings can be matched as literals within the regex. Example: \d{3}pie will match any three digits and the word pie. Commented Mar 28, 2014 at 23:05

2 Answers 2

3

Your if statement is not attempting to match a regular expression pattern, it's simply comparing two strings. That's what the Regex.IsMatch method is for. Also you will probably want to use Regex.Escape to combine a regex pattern with an arbitrary string.

Try this:

using System.Text.RegularExpressions;

...

var pattern = @"\d{2,}\s" + Regex.Escape(myString);
var fileName = Path.GetFileNameWithoutExtension(dir);
if (Regex.IsMatch(fileName, pattern))
{ 
    // do stuff 
} 
Sign up to request clarification or add additional context in comments.

Comments

0

You actually shouldn't have to use a Regular Expression in that instance, in fact that may be over complicating the goal.

string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string [] file = Directory.GetFiles(path, @"11_*.txt", SearchOption.AllDirectories);
foreach(string f in file)
{
    // Do Something.
}

If your entirely headset on utilizing Regular Expressions, you would do something more along these lines:

using System.Text.RegularExpressions;

...

    string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
    var file = Regex.IsMatch(path, @"\d{2,}\2");
    if(file == true)
    {
         // Do Something
    }

That is one way to use Regular Expressions, they can be even more powerful with Linq. I would highly recommend researching this on the MSDN, it has some solid resources.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.