Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

248
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!