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 write embedded flag expression?

It’s also possible to enable various flags using embedded flag expressions. Embedded flag expressions are an alternative to the two-argument version of compile, and are specified in the regular expression itself. The example below is use (?i) flag expression to enable case-insensitive matching.

Another flag expressions are listed below:

  • (?x), equivalent with Pattern.COMMENTS
  • (?m), equivalent with Pattern.MULTILINE
  • (?s), equivalent with Pattern.DOTTAL
  • (?u), equivalent with Pattern.UNICODE_CASE
  • (?d), equivalent with Pattern.UNIX_LINES


The result of the program is: