How do I make a regex capture a large variation of a term? (House House)

1

It is as follows: since I have a term that I want to capture, for example, "HOUSE", how to make a regex so that many variations of that word can be captured,

  

Home, CASa, CAsa, house, CaSA, CasA ...

I know there are

  

(Home | CASa | CAsa | Home | CaSA | CasA)

But notice that for this it is necessary that I write all the existing variations of the word, which would be 16 for "HOUSE". Depending on the word, this would become a Herculean task. Do regexs have a mechanism that makes our lives easier on that?

    
asked by anonymous 26.12.2016 / 20:07

2 answers

5

You can use:

/(casa)/ig

Explanation:

  • (word): Character capture group literally
  • / i: insensitive modifier
  • / g: modifier for global

For Java, as commented @AronLinhares, it should be:

(?i)(casa)

Regexr: link

    
26.12.2016 / 20:32
2

You can use the CASE_INSENSITIVE flag. See this example working on ideone :

import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        Pattern p = Pattern.compile("casa", Pattern.CASE_INSENSITIVE);
        System.out.println(p.matcher("Casa").matches());
        System.out.println(p.matcher("CaSa").matches());
        System.out.println(p.matcher("CAsA").matches());
        System.out.println(p.matcher("casa").matches());
        System.out.println(p.matcher("CaSA").matches());
        System.out.println(p.matcher("Sopa").matches());
        System.out.println(p.matcher("Carro").matches());
        System.out.println(p.matcher("Verde").matches());
    }
}
    
26.12.2016 / 20:31