How-to articles, tricks, and solutions about REGEX

Python string.replace regular expression

You can use the re module in Python to use regular expressions with the replace() method.

Regex pattern including all special characters

To create a regular expression that includes all special characters, you can use the following pattern:

Remove all non-numeric characters from a string; [^0-9] doesn't match as expected

You can use the preg_replace function in PHP to remove all non-numeric characters from a string.

Remove all special characters, punctuation and spaces from string

Here is an example of code that removes all special characters, punctuation, and spaces from a string in Python:

Remove control characters from PHP string

In PHP, you can use the preg_replace function to remove control characters from a string.

Remove HTML tags from a String

To remove HTML tags from a string, you can use a regular expression to match and replace the tags with an empty string.

Remove new lines from string and replace with one empty space

To remove new lines from a string and replace them with a single empty space in PHP, you can use the str_replace function.

Split Java String by New Line

To split a string in Java by new line, you can use the split method of the String class and pass it the regular expression "\n" or "\r\n", depending on the platform you are running on.

Split string on whitespace in Python

You can use the split() method to split a string on whitespace in Python.

Whitespace Matching Regex - Java

In Java, you can use the following regular expression to match any amount of whitespace:

1 2