Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java method that only selects the Regexp match instead of replacing it?

Java doesn’t seem to have a method where I can only get the matched string.

'content.append(line.replaceAll("(?<=name=\").*(?=\")", ""));` 

(this only leaves (name="") It worked on those online tools to select the name itself within the quotes but there’s no method that I can use to set my string to only the pattern that matches, instead of the pattern being replaced by something else.

It just leaves name="", but I want to remove name="" only, and leave the actual name inside the quotes.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Any ideas on a method that won’t require me to rewrite this over and over again.

Here’s the document I’m trying to edit, in case it helps:

version="1.4.4.2"
tags={
    "Total Conversion"
}
name="Princes of Darkness"
supported_version="1.4.*"
path="C:/Users/Flint/Documents/Paradox Interactive/Crusader Kings III/mod/princesofdarkness"
remote_file_id="2216659254"

I only want – Princess Of Darkness in my string, without any quotes.

>Solution :

You can use Matcher#find and Matcher#group.

Matcher matcher = Pattern.compile("(?<=name=\").*(?=\")").matcher(line);
if (matcher.find()) 
    content.append(matcher.group());
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading