Check if string is Match GetInvalidFileNameChars() [duplicate] - c#

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.

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#

how can i extract a specific part of a string in c# with regex.match? [duplicate]

This question already has answers here:
Regex Match all characters between two strings
(16 answers)
Closed 3 years ago.
Though this is probably a duplicate, i haven't been able to make this work after reading similar questions. Looking for help please with this homework, i'm a newbie at programming.
I'm working on a program in C#. I have a text that contains for example this sentence: "My homework version V0.90 from". I need to extract "V0.90", and that may vary from anything between V0.90 to V2.00. It is always surrounded by "My homework version " and " from", i need to extract whatever is between that. This is what i've tried:
string RetRegMatch;
Match Match1 = Regex.Match(Friends[a], #"My homework version (.+) from", RegexOptions.IgnoreCase);
RetRegMatch = Match1.Value;
But i'm geeting as a result in Match1.Value this: "My homework version V0.90 from", and i only want the (+.) part to be in Match1.Value. That is, i would like to have in Match1.Value this: "V0.90". How can i do this?
The part you need should be in the capture group (the part you put between ()). Try accessing it with
Match1.Groups[1].Value

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

How to get the value of a regex capture group? [duplicate]

This question already has answers here:
How to read RegEx Captures in C#
(3 answers)
Closed 7 years ago.
First of all sorry about the tittle. Didn't know how to explain it.
This is my code:
string sString = #"docs/horaires/1/images/1"
var PickImage = Regex.Matches(sString, "/horaires/(.*?)/images/");
Console.WriteLine(PickImage[0].Value);
This will print /horaires/1/images/ instead of 1. I tried all the RegexOptions but didn't find the solution in there.
What am I doing wrong here?
This is what you need:
string sString = #"docs/horaires/1/images/1";
var pickImage = Regex.Match(sString, #"/horaires/(.*?)/images/");
if (pickImage.Success)
Console.WriteLine(pickImage.Groups[1].Value);
In your original code, PickImage[0] is a Match object, and Value will return the full match. You want the first captured group, so use match.Groups[1].Value. Note that Groups[0] always contains the full match.
No need to use Matches is you want a single result, use Match instead.

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

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

Categories

Resources