Removing unwanted characters from folder names [duplicate] - c#

This question already has answers here:
How to remove illegal characters from path and filenames?
(30 answers)
Closed 9 years ago.
I'm creating bunch of folders using a C# console application. An XML file is parsed for different nodes and based on the values the folders are created with the same name.
One of the XML node had the following value with some unknown special character in it (ASCII code 127)
There is a special character after Foldername. I tried using String.Trim() to trim the value but had no luck. I also tried to compare the character with the list of
System.IO.Path.GetInvalidFileNameChars()
and remove it. But still no luck. How can I try to eliminate these characters before I create a folder name. The folder name will be always alpha numeric in my case.

If the folder name will "always be alpha numeric", then you can simply remove all non-alphanumeric characters:
var regex = new Regex("[^a-zA-Z0-9]");
fileName = regex.Replace(fileName, string.Empty);

You could remove the unwanted characters using Regular Expressions -
string validFolderName = Regex.Replace(folderName,"[^A-Za-z0-9 _]","");

Related

How to replace only last two sign to nothing using method Replace [duplicate]

This question already has answers here:
Replace the last occurrence of a word in a string - C#
(7 answers)
Closed last year.
I've got string "55, 55,55, 55, " and I want to replace last two characters (comma and white space) to nothing using only the method .Replace(). The example below clear all commas but I want to clear only two last ones. In Java you write:
variable.replaceAll(", $",""); // it's correct effect two last characters are changed to nothing
Replace(", ","") // but in c# you get: 55555,5555 and Replace(", $","") doesn't work
How to write it properly to get this effect using only Replace method?
Use Regex.Replace:
Regex.Replace(input, ", $", "");
If it is always two characters, you could also combine String.EndsWith and String.Substring, which is likely cheaper than compiling and applying a regex:
if (input.EndsWith(", ")) {
input = input.Substring(0, input.Length - 2);
}

c# - Split Comma separated String with nested children in brackets [duplicate]

This question already has answers here:
How to split string while ignoring portion in parentheses?
(5 answers)
Closed 1 year ago.
I have a nested comma string such as
a(x,y,z),b,c(n,o,p),d,e,f(t,w)
Want to split this string in C# such as
a(x),a(y),a(z),b,c(n),c(o),c(p),d,e,f(t),f(w)
I tried splitting using combination String.Split & String.SubString. Please let me know f you any solution for this problem.
Many problems get easier if you split them into smaller problems. This is one of them.
Step 1: Split on , while ignoring separators in parenthesis (see this related question for a regex-based solution: How to split string while ignoring portion in parentheses?)
This will yield a(x,y,z), b, c(n,o,p), ...
Step 2: Split the part before and inside the parenthesis (using a regular expression or just String.Split), split the inside part on ,, loop through it and add the component before the parenthesis.
This will transform a(x,y,z) into a(x), a(y), ...

Remove certain Unicode (garbage) JSON characters [duplicate]

This question already has answers here:
How can you strip non-ASCII characters from a string? (in C#)
(15 answers)
C# regex to remove non - printable characters, and control characters, in a text that has a mix of many different languages, unicode letters
(4 answers)
Closed 4 years ago.
I'm reading data from a file, and sometimes the file contains funky stuff, like:
"䉌Āᜊ»ç‰ç•‡ï¼ƒè¸²æœ€ä²’Bíœë¨¿ä„€å•²ï²ä‹¾é¥˜BéŒé“‡ä„€â²ä‹¾â¢"
I need to strip/replace these characters as JSON has no idea what to do with them.
They aren't control characters (I think), so my current regex of
Regex.Replace(value, #"\p{C}+", string.Empty);
Isn't catching them.
A lot of these strings read in are going to be long, upwards of256 characters, so I'd rather not loop through each char checking it.
Is there a simple solution to this? I'm thinking regular expressions would solve it, but I'm not sure.
If all you want is ASCII then you could do:
Regex.Replace(value, #"[^\x00-\x7F]+", string.Empty);
and if all you want are the "normal" ASCII characters, you could do:
Regex.Replace(value, #"[^\x20-\x7E]+", string.Empty);

Regex to match XML elements in a text file [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
RegEx match open tags except XHTML self-contained tags
I have a text file consist of conversion instruction templates.
I need to parse this text file,
I need to match something like this:
(Source: <element>)
And get the "element".
Or this pattern:
(Source: <element attr="name" value=""/>)
And get "element attr="name"".
I am currently using this regex:
\(Source:\ ?\<(.*?)\>\)
Sorry for being a newbie. :)
Thanks for all your help.
-JRC
Try this Regex for detect attibs by both ” or " characters:
\(Source:\s+<(\w+\s+(?:\w+=[\"”][^\"”]+[\"”])?)[^>]*>\)
and your code:
var result = Regex.Match(strInput,
"\\(Source:\\s+<(\\w+\\s+(?:\\w+=[\"”][^\"”]+[\"”])?)[^>]*>")
.Groups[1].Value;
explain:
(subexpression)
Captures the matched subexpression and assigns it a zero-based ordinal number.
?
Matches the previous element zero or one time.
\w
Matches any word character.
+
Matches the previous element one or more times.

Splitting a comma delimited text file [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Reading csv file
I have a comma delimited file:
"Some Text, More Text", 1, 2, 3,4,5,6
"Random Text, text text", 2,4,5,6,7,8
var content = reader.ReadLine();
var stringArray = content.Split(',');
The problem is the text ends up being split into two parts. I want to keep it as one unit. So what are my options?
EDIT: I meant like
Some Text
More Text
1
2
3
4
5
6
I want it like
Some Text,More Text
1
2
3
4
5
6
How about finding all the matches of this regex:
"[^"]*"|\S+
I usually use the Microsoft.VisualBasic.FileIO.TextFieldParser object, see:
http://msdn.microsoft.com/en-us/library/f68t4563.aspx
and example of implementation at:
http://www.siccolo.com/Articles/CodeProject/Open_DataSet_From_TextFile/open_dataset_from_text_csv_file.html
This allows me to handle CSV files without worrying about how to cope with whether fields are enclosed in quotes, contain commas, escaped quotes etc.
You need to use Regex in Split, so that text in quotes are excluded!

Categories

Resources