简体   繁体   中英

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 :

  1. Set the content of blogDescription field into description field
  2. 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=;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM