0

I have the following text I'm searching:

<add key="Directory" value="" />

And this is my regex:

add key=\x22Directory\x22 value=\x22([^\x22]*)\x22

My desired output is:

<add key="Directory" value="blue" />

But, with what I have, it seems to be selecting the whole tag and producing:

<blue />

But, according to regex101, it shouldn't be. What am I doing wrong?

I have also tried:

value=\x22([^\x22]*)\x22

But, that returns:

<add key="Directory" blue />

Regex101 is telling me the following. It matches and notices the first capturing group but yet is still replacing the entire value=""

regex submatch

5
  • The "producing" part of your post isn't currently showing up so it's hard to know what you're asking. Commented Mar 4, 2015 at 22:43
  • Sorry, updated my question. Commented Mar 4, 2015 at 22:47
  • Don't use RegEx to parse XML. Use the DOM and XPath/CSS selectors if possible or another XML Api if needed. Commented Mar 5, 2015 at 9:47
  • @ThW, I agree but sadly I am writing a batch file and this is the best thing I found. jrepl. Commented Mar 5, 2015 at 17:30
  • msdn.microsoft.com/en-us/library/ms764708%28v=vs.85%29.aspx Commented Mar 5, 2015 at 17:34

1 Answer 1

1

You want to use lookaround. So something like this, where the spot you're trying to pinpoint is preceded and proceeded by the lookaround text:

(?<=<add key="Directory" value=")[^"]*(?=" />)

Tweak to your liking, but that's the basic idea.

EDIT:

Since using jscript, try something like this (tweaking the substitution):

var re = /add key=\x22Directory\x22 value=\x22([^\x22]*)\x22/; 
var str = '<add key="Directory" value="" />'; //<- sample input. it could be your whole XML doc.
var subst = 'add key="Directory" value="Blue"'; 

var result = str.replace(re, subst);

or...

var re = /(add key=\x22Directory\x22 value=\x22)[^\x22]*(\x22)/; 
var str = '<root>\n    <someOtherElement/>\n    <add key="Directory" value="" />\n</root>';
var subst = '$1Blue$2'; 

var result = str.replace(re, subst);
Sign up to request clarification or add additional context in comments.

5 Comments

Hmm that seems to result in a syntax error in regular expression: i.imgur.com/JnQRQgA.png
Your test tool may not support lookbehind. What technology will you ultimately be running the expression in? .NET will work fine. FWIW - The verbatim expression ran great in Expresso, which is what I use for testing.
I will be using JScript (jrepl specifically) if that helps in anyway. I've been testing with regex101 which also complained of syntax errors. I was able to fix the syntax errors by removing the < you had in the first lookaround.But, it also didn't work
Edited my answer. If you use your original expression that matches the whole thing, you can just have the substitution include the original value + your added attribute value.
I also included a second option where you invert what you capture, then perform the substation on what's in-between via: '$1Blue$2' where $1 is the first group you capture in parenthesis "Blue" is the value you want to stick in-between and $2 is the second group you capture via the modified expression (whose parenthesis have changed since the original expression like "(add key=\x22Directory\x22 value=\x22)[^\x22]*(\x22)").

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.