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.
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.
CodePudding user response:
You can use Matcher#find
and Matcher#group
.
Matcher matcher = Pattern.compile("(?<=name=\").*(?=\")").matcher(line);
if (matcher.find())
content.append(matcher.group());
CodePudding user response:
To remove name=
, do this:
line.replaceAll("^name=(?=\\")", "")
This matches name=
when at the start of the line (via ^
) and when the following character is a quote (via (?=\")
).
In context of your code:
content.append(line.replaceAll("^name=(?=\\")", ""));