Regex to get a text between

1

I'm trying to get a word between <> . For example:

Text: "Joao <[email protected]>"

My regex should catch [email protected] , but it's catching <[email protected]> The regex I am using is <(.*?)> .

Does anyone know how to remove <> ?

    
asked by anonymous 13.11.2015 / 20:46

4 answers

1
  

Does anyone know how to remove < >?

Yes, you have to escape them.

In java ...

public static void main(String[] args) {

    String texto = "Joao <[email protected]>";
    String regex = "\<(?<meuGrupo>.*?)\>";
    String retorno = "";

    Pattern pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);;
    Matcher comparator = pattern.matcher(texto);
    if (comparator.find(0)){
        retorno = comparator.group("meuGrupo");
    }

    System.out.println(retorno);
}
    
13.11.2015 / 21:26
4

Workaround (without RegEx)

As the author mentioned using Java, there is an alternative answer with substring , for anyone who might be interested:

mail = mail.substring(mail.indexOf("<")+1,mail.indexOf(">"));

See working at IDEONE .

    
13.11.2015 / 22:09
2

You can use an expression that reads all characters other than > :

\<([^\>]+)\>

A JSFiddle showing an example with this regex: link . Note that you do not have to escape < and > , but I usually escape all special characters to avoid confusion.

    
13.11.2015 / 21:04
1

For me it worked using the regular expression <(.*)> .

Using link - testing this regular expression with Joao <[email protected]> , what was captured was just [email protected] .

    
13.11.2015 / 21:06