DarkW1nter
DarkW1nter

Reputation: 2851

evaluate filename with regex

I have a bit of code to evaluate a filename using a regex, this works fine, but I want to add in a 2nd pattern of out_\d\d\d\d\d\d_ (then up to 150 character to hold an address). Obviously I don't want to have \d 150 times, can anyone tell me the best way to to this?

thanks

REGEX_PATTERN = @"out_\d\d\d\d\d\d";

if (!Regex.Match(Path.GetFileNameWithoutExtension(e.Name), REGEX_PATTERN).Success) {
  return;
}

Upvotes: 0

Views: 120

Answers (2)

Bob Vale
Bob Vale

Reputation: 18474

You want:

REGEX_PATTERN = @"^out_\d{6}(?:_.{1,150})?$";

This breaks down as

`^`             - start of string
`out_\d{6}`     - `out_` followed by 6 digits
`(?:_.{1,50})?` - an optional string of _ followed by 1-150 characters
`$`             - end of string

Upvotes: 1

Furqan Safdar
Furqan Safdar

Reputation: 16698

Try this out:

REGEX_PATTERN = @"out_\d{1,150}";

OR

// For strict boundary match
REGEX_PATTERN = @"^out_\d{1,150}$";

Upvotes: 1

Related Questions