Regexp to get the content of a string between two quotes, starting with a given name
I have multiple lines in an xml file.
My lines are like <Blog blogDescription="bla bla bla" description="" date="2010-10-10"/>
I'm working on all lines starting with "<Blog"
where I want to :
- Set the content of bl开发者_StackOverflow社区ogDescription field into description field
- Remove blogDescription field
So my line would be like :
<Blog description="bla bla bla" date="2010-10-10"/>
I don't know what kind of regexp i can use, I only get the line with :
"^<(Blog) .*"
And I remove blogDescription field with :
" blogDescription="
But I don't know how to put the blogDescription
value into description value.
If you're already working with XML that is correctly formatted, rather than building a parser yourself via regex, why not just use one of the XML parsers available to you? There are many available to do this.
See this related question: Parsing XML in Java
String val = "<Blog blogDescription=\"bla bla bla\" description=\"\" date=\"2010-10-10\"/>";
String regex = "^<Blog (blogDescription=\"[^\"]*\"\\s+).*";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(val);
matcher.matches();
MatchResult result = matcher.toMatchResult();
System.out.println(result.group(1));
String resultString = val.replace(result.group(1), "");
System.out.println(resultString);
you can use like this:
String str = "<Blog blogDescription=\"bla bla bla\" description=\"\" date=\"2010-10-10\"/>";
System.out.println(str.replaceAll("blogDescription=\"([^\"]+)\"\\s+description=\"[^\"]*\"",
"description=\"$1\""));
.I don't know if there is any newline in the string. it would not work if you have one newline in the string liking: blogDescription="bla \nbla"\n description=;
精彩评论