Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

249
Visualizações
Get a specific word out of a String with regex

I have an String

String string = "-minY:50 -maxY:100 -minVein:8 -maxVein:10 -meta:0 perChunk:5;";

And I want to somehow get the -meta:0 out of it with regex (replace everything except -meta:0), I made an regex which deletes -meta:0 but I can't make it delete everything except -meta:0 I tried using some other regex but it was ignoring whole line when I had -meta:[0-9] in it, and like you can see I have one line for everything. This is how it has been deleting -meta:0 from the String:

String meta = string.replaceAll("( -meta:[0-9])", "");
System.out.println(meta);

I just somehow want to reverse that and delete everything except -meta:[0-9]

I couldn't find anything on the page about my issue because everything was ignoring whole line after it found the word, so sorry if there's something similar to this.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You should be capturing your match in a captured group and use it's reference in replacement as:

String meta = string.replaceAll("^.*(-meta:\\d+).*$", "$1");
System.out.println(meta);
//=> "-meta:0"

RegEx Demo

over 4 years ago · Santiago Trujillo Relatório

0

As I understand your requirement you want to :

a) you want to extract meta* from the string

b) replace everything else with ""

You could do something like :

String string = "-minY:50 -maxY:100 -minVein:8 -maxVein:10 -meta:0 perChunk:5;";


Pattern p = Pattern.compile(".*(-meta:[0-9]).*");
Matcher m = p.matcher(string);

if ( m.find() )
{
    string = string.replaceAll(m.group(0),m.group(1));

    System.out.println("After removal of meta* : " + string);
}

What this code does is it finds meta:[0-9] and retains it and removes other found groups

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda