How to escape all metacharacters using Pattern.compile (..)?

3

File:

  

ftp: //paginaqualquer.html

     

link

     

link

My code:

public static void main(String[] args){
     //Supondo que o readLine() esteja no loop != null
     String arquivoLinha = arquivo.readLine();
     Pattern padrao = Pattern.compile(arquivoLinha); //O problema está aqui
     Matcher texto = padrao.matcher("http://minhapagina.php?id=");
     while (texto.find()){
          System.out.println(texto.group());
     }
}

When the last line arrives in the variable file Row ( link ), what I did not want to happen happened: The metacharacter "?" has power, thus modifying my research. I could even use the "\" escaper to ignore the "?", But how do I do that? Or at least ignore any metacharacters that are in the String file variable and consider it as literal.

I tried to do this:

Pattern.compile("["+"("+arquivoLinha+")"+"]");

Using the line inside the group, that within the list to ignore the metacharacters, but that is not working very well.

    
asked by anonymous 27.03.2016 / 00:06

1 answer

4

You need the compiled pattern to be literal , that is, that meta characters or escape characters in the pattern do not have regular expression handling.

To do this simply compile the pattern by telling a flag saying this, that "escape" such characters. You find this flag in the Pattern itself, the LITERAL .

To compile the pattern using this flag just do this:

final Pattern padrao = Pattern.compile("http://minhapagina.php?id=", Pattern.LITERAL);
final Matcher texto = padrao.matcher("http://minhapagina.php?id=");
while (texto.find()) {
    System.out.println(texto.group());
}

This will make your pattern "case" as text, then this will print:

http://minhapagina.php?id=
    
27.03.2016 / 15:37