开发者

How do I substitute a parameter of an XML tag... using Regex?

Help how do I use regex to replace the value of param below

<?xml version="1.0" encoding="UTF-8" ?>
<games>
    <game id="1001" path="C:\Program Files\Warcraft III\war3.exe" param="" display="1" priority="0"/>
</games>

the value of param is empty i wanted to add something to it using regex. or replace the hole param="" with param="something"

and it has to be the first param after id="1001" help.

i'm also using autohotkey so.. I don't know if you can just provide me a code to edit xml with autohotkey. :P but regex would do for this.

somebody provided me with this code

RegExReplace(xml,"s)id=""1001开发者_StackOverflow"".*?param=""\K[^""]+","HELLO WORLD!")

it works if the param has a value but it won't work if it doesn't. how do i make it work.


You could use something like this, but you should consider using a proper XML parser instead, since this regex will easily fail in many cases:

s/(id="1001" [^>]*param=").*?"/$1something"/


You might be better off looking at an XML/HTML parsing engine here, assuming you are talking about XML/HTML params. Such engines are made for parsing and modifying this kind of content - regexes are not at all ideal for such work.

But it would help to know more about what you are dealing with, too; what's the environment? Is this HTML/XML data? Where are you modifying it? (client? server?) etc.


If the command you posted works as you said, then all you need to do is change the + to a *, like so:

RegExReplace(xml,"s)id=""1001"".*?param=""\K[^""]*","HELLO WORLD!")

+ means "one or more; * means "zero or more"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜