I want a regex to find string between two characters but only from start delimiter to first occurrence of end delimiter
I want to extract story from the lines of following format
<metadata name="user" story="{some_text_here}" \/>
So I want to extract only : {some_text_here}
For that I am using the following regex:
<metadata name="user" story="(.*)" \/>
And java code:
public static void main(String[] args) throws IOException {
String regexString = "<metadata name="user" story="(.*)" \/>";
String filePath = "C:\\Desktop\\temp\\test.txt";
Pattern p = Pattern.compile(regexString);
Matcher m;
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = br.readLine()) != null) {
m = p.matcher(line);
if (m.find()) {
System.out.println(m.group(1));
}
}
}
}
This regex mostly works fine but surprisingly if the line is:
<metadata name="user" story="My name is Nick" extraStory="something" />
Running the code also filters My name is Nick" extraStory="something
where as I only want to make sure that I get My name is Nick
Also I want to make sure that there is actually no information between story="My name is Nick"
and before />