C#, Regex, string of a specific length and pattern [duplicate] - c#

This question already has answers here:
Is there a way to test if a string is an MD5 hash?
(6 answers)
Closed 8 years ago.
I've got a problem with checking whether the string is md5 or not. I know that it should contain "0123456789ABCDEF" and its length equals to 32. I know that regex is a good way to check it, but I have no idea to use it. I tried searching on msdn but nothing cleared my mind. Can anyone help me?

Use Regex.IsMatch and the regex [0-9a-f]{32}:
if (Regex.IsMatch(string, #"[0-9a-f]{32}"), RegexOptions.IgnoreCase))
//String is MD5

Related

Eliminating white spaces in strings [duplicate]

This question already has answers here:
Efficient way to remove ALL whitespace from String?
(18 answers)
Closed 3 years ago.
I'm looking for a efficient way for removing all of the white spaces in an string.
I have checked replace (replace(' ','')) but I'm looking for a more efficient way.
I'd appreciate the help.
You may use Regular Expression.
For example:
var result=System.Text.RegularExpressions.Regex.Replace(input, #"\s+", "");
Input is your string
See more Removing whitespaces using C#

C# Regex - match a substring in a filename [duplicate]

This question already has answers here:
Reference - What does this regex mean?
(1 answer)
Given a filesystem path, is there a shorter way to extract the filename without its extension?
(10 answers)
Closed 4 years ago.
My regex is really poor so I need help with a c# regex expression that can match a substring after the last backslash.
Typical input:
D:\DataFiles\Files_81\aars2016FAKH1800010.pdf
I need to check if the filename aars2016FAKH1A800010.pdf contains "FAKH1". It is important that only the filename is evaluated.
It must be done with C# regex, so please no "Contains"
You might be wondering why regex, but this is going to be used in a generic c# application that can evaluate regex expressions.
Thank you in advance.
You can try to use \\\w*(FAKH)\w*\.pdf pattern.
bool isExsit = Regex.IsMatch(#"D:\DataFiles\Files_81\aars2016FAKH1800010.pdf", #"\\\w*(FAKH)\w*\.pdf");
EDIT
You can use Groups[1].Value get FAKH
var result = Regex.Match(#"D:\DataFiles\Files_81\aars2016FAKH1800010.pdf", #"\\\w*(FAKH)\w*\.pdf");
var FAKH = result.Groups[1].Value;
c# online

Regex Pattern to check only letters [duplicate]

This question already has answers here:
C# Regex to allow only alpha numeric
(6 answers)
Closed 5 years ago.
I need to check whether string is only contain letters but not numbers or special characters.I used below regex pattern,
String validText = "^[a-zA-Z-]+$";
its work fine for 'Leo#' but if it is like 'Leo#1' its not working properly.
Anyone have idea ?
I prefer you can use LinQ (input is your test string)
bool result = input.All(Char.IsLetter);
Else as Gordon Posted the right Regex,
^[a-zA-z]+$
You can try using this regex
/^[A-Za-z]+$/
This will match only letters in your string ..

Regex is not matching, no idea why [duplicate]

This question already has answers here:
How can I deserialize JSON with C#?
(19 answers)
Closed 5 years ago.
Just a quick one, I'm probably just blind. I'm using regex to match bits of JSON data. I have this line:
String ErrorMessage = new Regex("\"message\":\"\\w+\"").Match(response).Value;
Which should hopefully match something like this:
"message":"This is a messsage"
But it isn't matching it at all, I know it's in there though.
Thanks in advance.
EDIT: While this might not be best practice, it's the first time that I've used both regex and JSON, and so didn't want to use any external APIs or libraries. I'll be sure to use something better in the future :)
This is not a duplicate, as I am not asking how to parse JSON, I'm simply asking why a regex doesn't match a pattern. Thanks.
Thanks for the answers!
The \w character class does not include the space character.

Check if string is Match GetInvalidFileNameChars() [duplicate]

This question already has answers here:
How do I check if a given string is a legal/valid file name under Windows?
(27 answers)
Closed 7 years ago.
I try to check if string is Match GetInvalidFileNameChars()
i would like to use regex
So i put the chars of GetInvalidFileNameChars() into a string and then check
if Regex.IsMatch(id, stringInvalidFileName)
I thought if id = "4711./" then Regex.IsMatch(id, stringInvalidFileName)
should be true, but it's false
What is my mistake, why is it false ?! Thanks in advance
Why use a regex?
This will work fine:
string s = "4711./";
if (Path.GetInvalidFileNameChars().Any( c => s.Contains(c))
As Rawling points out below, when you're dealing with large strings it might be more efficient to use Intersect instead:
string longString = "Something much, much longer than this";
if (longString.Intersect(Path.GetInvalidFileNameChars()).Any())
For relatively short strings (file paths, for example) there's probably little if any benefit. I'd prefer the first option, as it more clearly conveys the code's intent, in my opinion.

Categories

Resources