How to truncate a string after n number of words?

The result of the snippet:

How to remove non ASCII characters from a string?

The code snippet below remove the characters from a string that is not inside the range of x20 and x7E ASCII code. The regex below strips non-printable and control characters. But it also keeps the linefeed character \n (x0A) and the carriage return \r (x0D) characters.

Snippet output:

How do I compile character classes with quantifier?

This example show you how to attach quantifier to character classes or capturing group in regular expressions.

This program will print the following output: