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;
}
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
Try this out:
REGEX_PATTERN = #"out_\d{1,150}";
OR
// For strict boundary match
REGEX_PATTERN = #"^out_\d{1,150}$";
Related
I want to validate a filename with this format : LetterNumber_Enrollment_YYYYMMDD_HHMM.xml
string filename = "Try123_Enrollment_20130102_1200.xml";
Regex pattern = new Regex(#"[a-zA-z]_Enrollment_[0-9]{6}_[0-9]{4}\\.xml");
if (pattern.IsMatch(filename))
{
return isValid = true;
}
However, I can't make it to work.
Any thing that i missed here?
You are not matching digits at the beginning. Your pattern should be: ^[A-Za-z0-9]+_Enrollment_[0-9]{8}_[0-9]{4}\.xml$ to match given string.
Changes:
Your string starts with alphanumeric string before first _ symbol so you need to check both (letters and digits).
After Environment_ part you have digits with the length of 8 not 6.
No need of double \. You need to escape just dot (i.e. \.).
Demo app:
using System;
using System.Text.RegularExpressions;
class Test {
static void Main() {
string filename = "Try123_Enrollment_20130102_1200.xml";
Regex pattern = new Regex(#"^[A-Za-z0-9]+_Enrollment_[0-9]{8}_[0-9]{4}\.xml$");
if (pattern.IsMatch(filename))
{
Console.WriteLine("Matched");
}
}
}
Your Regex is nowhere near your actual string:
you only match a single letter at the start (and no digits) so Try123 doesn't match
you match 6 digits instead of 8 at the date part so 20130102 doesn't match
you have escaped your backslash near the end (\\.xml) but you've also used # on your string: with # you don't need to escape.
Try this instead:
#"[a-zA-Z]{3}\d{3}_Enrollment_[0-9]{8}_[0-9]{4}\.xml"
I've assumed you want only three letters and three numbers at the start; in fact you may want this:
#"[\w]*_Enrollment_[0-9]{8}_[0-9]{4}\.xml"
You can try the following, it matches letters and digits at the beginning and also ensures that the date is valid.
[A-Za-z0-9]+_Enrollment_(19|20)\d\d(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])_[0-9]{4}\.xml
As an aside, to test your regular expressions try the free regular expression designer from Rad Software, I find that it helps me work out complex expressions beforehand.
http://www.radsoftware.com.au/regexdesigner/
Ok so i have these strings
location = "C:\\Users\\John\\Desktop\\399";
location = "C:\\Users\\John\\Desktop\\399\\DISK1";
location = "C:\\Users\\John\\Desktop\\399\\DISK2";
location = "\\somewhere\\on\\Network\\399\\DISK2";
how do i strip out the 399 from all these situations ....FYI the number might be 2 digits like 42 so i cant grab the last 3 in the first case....i was thinking of some regex that would take out the DISKn if it exists and grab the number till the \ before the number but i dont know how to do that in C#...any ideas
Here is how to do this with Regex against your example input:
Regex rgx = new Regex("\\\d+");
string result = rgx.Replace(input, string.Empty);
The regular expression will match on a \ followed by at least one digit and replace them. You need to be careful though, as it will not preserve the string if you have this pattern elsewhere in the string.
If your inputs are exactly as you have described, using string.Split can be much more efficient (assuming the portion you need to remove is always last of before last).
Update:
The regex I provided will work only if you have a single part of the path that starts with numbers, not multiples or paths that have begin with numbers but do not end with them.
The information you have provided is not enough to built a regular expression that will do as you wish - how do you distinguish between numeric paths that do need to be stripped out and those that do not, for example?
var parts = location.Split('\\');
var number = parts.Last().Starts("DISK") ? parts[parts.Length - 2] : parts[parts.Length - 1];
strip number out:
var index = parts.Last().Starts("DISK") ? parts.Length - 2 : parts.Length - 1;
var newParts = parts.Take(index).Concat(parts.Skip(index + 1)).ToArray();
var newLocation = string.Join("\\", newParts);
Take a look at the Split() method for breaking the string up around separators. Then you can use techniques such as checking for the last part starting with DISK, or checking for a part that is purely integer (possibly risky, in case higher subdirectories are pure numbers - unless you work from the back!).
int i = int.Parse(location.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries)[4]);
How can I get the string before the character "-" using regular expressions?
For example, I have "text-1" and I want to return "text".
So I see many possibilities to achieve this.
string text = "Foobar-test";
Regex Match everything till the first "-"
Match result = Regex.Match(text, #"^.*?(?=-)");
^ match from the start of the string
.*? match any character (.), zero or more times (*) but as less as possible (?)
(?=-) till the next character is a "-" (this is a positive look ahead)
Regex Match anything that is not a "-" from the start of the string
Match result2 = Regex.Match(text, #"^[^-]*");
[^-]* matches any character that is not a "-" zero or more times
Regex Match anything that is not a "-" from the start of the string till a "-"
Match result21 = Regex.Match(text, #"^([^-]*)-");
Will only match if there is a dash in the string, but the result is then found in capture group 1.
Split on "-"
string[] result3 = text.Split('-');
Result is an Array the part before the first "-" is the first item in the Array
Substring till the first "-"
string result4 = text.Substring(0, text.IndexOf("-"));
Get the substring from text from the start till the first occurrence of "-" (text.IndexOf("-"))
You get then all the results (all the same) with this
Console.WriteLine(result);
Console.WriteLine(result2);
Console.WriteLine(result21.Groups[1]);
Console.WriteLine(result3[0]);
Console.WriteLine(result4);
I would prefer the first method.
You need to think also about the behavior, when there is no dash in the string. The fourth method will throw an exception in that case, because text.IndexOf("-") will be -1. Method 1 and 2.1 will return nothing and method 2 and 3 will return the complete string.
Here is my suggestion - it's quite simple as that:
[^-]*
This is something like the regular expression you need:
([^-]*)-
Quick tests in JavaScript:
/([^-]*)-/.exec('text-1')[1] // 'text'
/([^-]*)-/.exec('foo-bar-1')[1] // 'foo'
/([^-]*)-/.exec('-1')[1] // ''
/([^-]*)-/.exec('quux')[1] // explodes
I dont think you need regex to achieve this. I would look at the SubString method along with the indexOf method. If you need more help, add a comment showing what you have attempted and I will offer more help.
You could just use another non-regex based method. Someone gave the suggestion of using Substring, but you could also use Split:
string testString = "my-string";
string[] splitString = testString.Split("-");
string resultingString = splitString[0]; //my
See http://msdn.microsoft.com/en-US/library/ms228388%28v=VS.80%29.aspx for another good example.
If you want use RegEx in .NET,
Regex rx = new Regex(#"^([\w]+)(\-)*");
var match = rx.Match("thisis-thefirst");
var text = match.Groups[1].Value;
Assert.AreEqual("thisis", text);
Find all word and space characters up to and including a -
^[\w ]+-
Sorry doing this the fast way of asking all the regex experts out there :)
What regex for C# can I use that matches the end of a word + some number
Eg. End of string matches with "Story" + 0 or more numbers
"ThisIsStory1" = true
"ThisIsStory2000" = true
"ThisIsStory" = true
"Story1IsThis" = false
"IsStoryThis1" = false
Hope this makes sense
A regular expression that I could plug into C# would be fantastic.
Thanks
You'll need something like this Story[0-9]*$
So it matches for story (ignoring anything before it, you may need to add more), then for 0 or more numbers between story and the end of the string.
Try [A-Za-z]*Story[0-9]*.
If you want to check against a whole line, ^[A-Za-z]*Story[0-9]*$.
to match number of characters before story and then story???? use this:
"[A-Za-z]*(s|S)tory\d*$"
string compare = "story1withANumber";
Regex regex = new Regex(#"[A-Za-z]+Story[0-9]*");
if (regex.IsMatch(compare))
{
//true
}
regex = new Regex("[0-9]");
if (regex.IsMatch(compare))
{
//true
}
A .NET regex that meets your criteria is:
Story\d*\b
(Use the Regex.IgnoreCase option if desired.)
\b matches the word boundary, so Story1IsThis will not match, but ThisIsAStory1 will. The word 'Story' does not need to end the string, so the string "ThisIsAStory234 ThisIsNot" will match on Story234.
To test one alphanumeric string we usually use the regular expression "^[a-zA-Z0-9_]*$" (or most preferably "^\w+$" for C#). But this regex accepts numeric only strings or alphabet only strings, like "12345678" or "asdfgth".
I need one regex which will accept only the alphanumeric strings that have at-least one alphabet and one number. That is to say by the regex "ar56ji" will be one of the correct strings, not the previously said strings.
Thanks in advance.
This should do it:
if (Regex.IsMatch(subjectString, #"
# Match string having one letter and one digit (min).
\A # Anchor to start of string.
(?=[^0-9]*[0-9]) # at least one number and
(?=[^A-Za-z]*[A-Za-z]) # at least one letter.
\w+ # Match string of alphanums.
\Z # Anchor to end of string.
",
RegexOptions.IgnorePatternWhitespace)) {
// Successful match
} else {
// Match attempt failed
}
EDIT 2012-08-28 Improved efficiency of lookaheads by changing the lazy dot stars to specific greedy char classes.
Try this out:
"^\w*(?=\w*\d)(?=\w*[a-zA-z])\w*$"
There is a good article about it here:
http://nilangshah.wordpress.com/2007/06/26/password-validation-via-regular-expression/
This should work:
"^[a-zA-Z0-9_]*([a-zA-Z][0-9]|[0-9][a-zA-Z])[a-zA-Z0-9_]*$"
This will match:
<zero-or-more-stuff>
EITHER <letter-followed-by-digit> OR <digit-followed-by-letter>
<zero-or-more-stuff>
By ensuring you have either a digit followed by letter or a letter followed by digit, you are enforcing the requirement to have at least one digit and at least one letter. Note that I've left out the _ above, because it wasn't clear whether you would accept that as a letter, a digit, or neither.
Try this one ^([a-zA-z]+[0-9][a-zA-Z0-9]*)|([0-9]+[a-zA-z][a-zA-Z0-9]*)$
Simple is better. If you had a hard time writing it originally, you're (or some other poor sap) is going to have a hard time maintaining it or modifying it. (And I think that I see some possible holes in the approaches listed above.)
using System.Text.RegularExpressions;
boolean IsGoodPassword(string pwd){
int minPwdLen = 8;
int maxPwdLen = 12;
boolean allowableChars = false;
boolean oneLetterOneNumber = false;
boolean goodLength = false;
string allowedCharsPattern = "^[a-z0-9]*$";
//Does it pass the test for containing only allowed chars?
allowableChars = Regex.IsMatch(pwd, allowedCharsPattern , RegexOptions.IgnoreCase));
//Does it contain at least one # and one letter?
oneLetterOneNumber = Regex.IsMatch(pwd, "[0-9]")) && Regex.IsMatch(pwd, "[a-z]", RegularExpressions.IgnoreCase));
//Does it pass length requirements?
goodLength = pwd.Length >= minPwdLength && pwd.Length <= maxPwdLength;
return allowableChars && oneLetterOneNumber && goodLength;
}