Delete lines in a Wordlist.txt that contain specified chars

Hello community,
I started to learn Wolfram Mathematica v12.0 some days ago and now I want to solve a little Problem.

There is a Wordlist.txt with German, Chinese and some other signs/symbols in it.
Now I want to remove every line/word in it, that contains something different then “abc…, ABC…,123…, and !?”.

I did many exercises and one of them was about strings, but I don’t understand how to put those things together yet.

Thanks for some help, with best regards :3

p.s. sorry about my English, missed practicing a very long time :0

Please give an example that can be worked on

Hi Christian,

I made up some data

SeedRandom[1];
words = RandomWord["CommonWords", 20, Language -> "German"] // 
  Join[#, {"树", "指", "都", "鱼", "刻", "朵", "今天"}] &

includeChars = {CharacterRange["A", "Z"], CharacterRange["a", "z"], 
   CharacterRange["0", "9"], "!"} // Flatten;

words // Select[! StringContainsQ[#, Except[includeChars]] &]

(*
{"Verhandlungsgeschick", "baggern", "Separation", "jure", 
"Rechenschritt", "Kriegsbereit", "Riesenteleskop", "Statusbit", 
"Dreieck", "cineastisch", "demselben", "Andalusier"}
*)

Oh this won’t be easy…

So my first Idea was something like:

Take[WordList[Wordlist.txt], StringDelete[{"ÿ","þ", "\[Not]", "Ø", "\.b2", "©", "yY","\[PlusMinus]"}]]

The code looks crazy when I try to copy and paste it so please look at the Picture.

Maybe a Problem with extra symbols / Chinese chars?

So this only works with “normal” characters or numbers and you have to say which characters you want to delete,.
But I want to say which should stay in the file and also it does not delete the whole string only the character you put in the list.

So as you can see I’m not experienced with Mathematica, but I’m willing learn :smiley:

So etwas vielleicht

text = {"aaabcdabce", "uuu", "bbABCmmm", "123$77", "3333", "ffeg"}
Select[text, Not[StringFreeQ[#, {"abc", "ABC", "123"}]] &]
Select[text, StringFreeQ[#, {"abc", "ABC", "123"}] &]

Hi Hans Dolhaine, first of all many thanks but that’s not exactly what I meant.

I want to remove Chinese/Arabic characters from a text file. Only numbers and letters from the German alphabet should be kept. So to put it simply, there should only be a-z, A-Z, no umlauts and “!?”.

So e.g.
Wordlist.txt before ------> Monkey should! Banana? Dog 今 天 Mother 指

Wordlist.txt after -------> Monkey should! Banana? Dog

I think I picked a somewhat difficult exercise for a beginner, but it is har for me to let go of things that I set out to do.

Kind regards Christian

words = {“Monkey should!”, “Banana?”, “Dog”, “今 天” , “Mother 指”};
includeChars = {CharacterRange[“A”, “Z”], CharacterRange[“a”, “z”],
CharacterRange[“0”, “9”], “!”, “?”, " "} // Flatten;
words // Select[StringFreeQ[#, Except[includeChars]] &]

(* {"Monkey should!", "Banana?", "Dog"} *)

Hello Rohit,
its not to 100% what I want but pretty close.

Is it possible to take a Wordlist.txt compute the code and save the changes to the Wordlist.txt?

This would be a solutioen I can agree with for the moment :slight_smile:

As I said to Dolhaine, maybe this question was to complex for a beginner like me.
But I cant sleep until a problem is solved :smiley:

OK I dont understand everything you did yet but a lot of practice will clear things out :wink:

Thank you for helping me out.

Hi Christian,

I created a sample file wordlist.txt (see attached).

includeChars = {CharacterRange["A", "Z"], CharacterRange["a", "z"], 
    CharacterRange["0", "9"], "!", "?", " "} // Flatten;

wordList = Import["~/wordlist.txt", "Data"]

filtered = wordList // Select[StringFreeQ[#, Except[includeChars]] &]

Export["~/filteredList.txt", filtered]

The file filteredList.txt has the lines that only have the characters in includeChars.

wordlist.txt (143 Bytes)

Hello Rohit,

Yes this is exactly what I was thinking of and it works great.
Thank you for your time and effort.

Have a nice day.

Greetings,

Christian