How To Create Case Insensitive Regex? – POFTUT

How To Create Case Insensitive Regex?


Regex is used given text according to different and flexible patterns. It provides a lot of different patterns which can match given text or line. The default behavior of the regex is case sensitive which means upper and lowercase letters are interpreted as different. We can match regex case insensitive or ignore case sensitivity.

Case Insensitive As Grep Option

grep is very popular tool which is used to filter given text with different patterns. grep command also supports Regex or regular expressions and run regex as case sensitive by default. We can disable case-sensitive match with the -i option.

$ grep -i "poftut" hostnames
Case Insensitive As Grep Option
Case Insensitive As Grep Option

Case Insensitive As Regular Expression Option

Regex language also provides an opportunity to make given regex pattern to be case insensitive. We can use (?i) which means the given regex will be case insensitive.

poftut(?i)

Java

In Java programming language we can use Pattern class which can use Regex and provide CASE_INSENSTIVE as an option. In this example, we will create a Regex pattern named patter which will be case insensitive.

Pattern pattern = Pattern.compile("poftut", Pattern.CASE_INSENSITIVE);

Case Insensitive As Regular Expression

Regular expressions also support different patterns to specify numbers letters, upper case letters, lower case letters etc. We can combine upper and lower case letters. [a-z] means lowercase letters and [A-Z] means upper case letters. We can create case insensitivity like below from both upper and lower case letters.

[a-zA-Z]

LEARN MORE  Killing Linux Processes With killall Command

Leave a Comment