Converting a String to a List of Words?

Try this: import re mystr=”This is a string, with words!” wordList = re.sub(“[^\w]”, ” “, mystr).split() How it works: From the docs : re.sub(pattern, repl, string, count=0, flags=0) Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl. If the pattern isn’t found, string is returned unchanged. … Read more

Can grep show only words that match search pattern?

Try grep -o: grep -oh “\w*th\w*” * Edit: matching from Phil’s comment. From the docs: -h, –no-filename Suppress the prefixing of file names on output. This is the default when there is only one file (or only standard input) to search. -o, –only-matching Print only the matched (non-empty) parts of a matching line, with each … Read more

How do I generate a randomly selected word, or phrase, from a previously selected list of words and phrases in Javascript? [closed]

Put these items in an array. var array = [“Horse”, “Pig”, “Dog”, “Cat”, “Parrot”, “Iguana”]; Generate a random number. var randInt = randomGenerator(0, array.length – 1); ( Generating random whole numbers in JavaScript in a specific range? ) Use the random number to get an item from the array. var item = array[randInt]; Use document.getElementById … Read more