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

205
Visualizações
How to use extract substring using regex

I am new to java regex. I need to extract "com.mycomp.war.tasks.JmxMetricsTask" from the below line. How can i do with regex?

String test = "id=         com.mycomp.war.tasks.JmxMetricsTask      I run/id-geLh3hM1-1_2 [Svc--DAG]";

Is it too complicated? Is it possible by regex? I need to extract above line in return?

VG

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

0

You need to define your problem and requirements more thoroughly.

For the example you show, there is a much simpler solution:

String test = "id=         com.mycomp.war.tasks.JmxMetricsTask      I run/id-geLh3hM1-1_2 [Svc--DAG]";
String answer = test.substring(3).trim().split(" ", 2)[0];

Disclaimer: this might not work as intended for all of your possible inputs. This is why I say that you need to completely define your situation. If all of your inputs match the assumptions I made based on your one example, then this would work without using regular expressions.

over 4 years ago · Santiago Trujillo Relatório

0

There is no need for Regex. You can do it like

String test = "id=         com.mycomp.war.tasks.JmxMetricsTask      I run/id-geLh3hM1-1_2 [Svc--DAG]";
String subTest = "com.mycomp.war.tasks.JmxMetricsTask";
test.substring(test.indexOf(subTest), subTest.length() + test.indexOf(subTest));

But can you explain your actual requirements?? using above, you can get the required string part

over 4 years ago · Santiago Trujillo Relatório

0

Well, you could search for a similar question.;

regexp to match java package name

I modified the regex from the top answer to suit your case. I replace the ^…$ (line start/end) portion with \b (word boundaries).

import java.util.regex.*;

public class RegexTest {
    public static final String PACKAGE_PATTERN = "\\b[a-z][a-z0-9_]*(\\.[a-z0-9_]+)+[0-9a-z_]\\b";

    public static void main(String[] args) {
        String s = "id=         com.mycomp.war.tasks.JmxMetricsTask      I run/id-geLh3hM1-1_2 [Svc--DAG]";
        Pattern p = Pattern.compile(PACKAGE_PATTERN, Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(s);

        if (m.find()) {
            System.out.println(m.group()); // com.mycomp.war.tasks.JmxMetricsTask
        }
    }
}

Here's a live example using Regex 101: /\b[a-z][a-z0-9_]*(\.[a-z0-9_]+)+[0-9a-z_]\b/ig

https://regex101.com/r/RwJtLK/2


You could also just split by whitespace characters and grab the second token.

public class RegexTest {        
    public static void main(String[] args) {
        String s = "id=         com.mycomp.war.tasks.JmxMetricsTask      I run/id-geLh3hM1-1_2 [Svc--DAG]";
        String[] tokens = s.split("\\s+");

        System.out.println(tokens[1]); // com.mycomp.war.tasks
    }
}
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