Problem with Regular Expression Search

Questions about XML that are not covered by the other forums should go here.
psbentley
Posts: 15

Problem with Regular Expression Search

Fri Jul 10, 2015 11:39 pm

I'm having some trouble using the Regular Expression search feature in Find/Replace. When I type \ in the Find box, it pops up with a hint saying "The expression "\{\n\}" matches the text "{n}"." To test this, I entered "{n}" into my file and hit Find but I'm getting String not found as a result. I have Regular expression checked, so I'm not sure what the problem is.

What I'm trying to do is search for this:

xtrc="/w:document/w:body[1]/w:p[299]"

where the digits in the brackets are wildcards. I did some trials and could not get it to work and then after it couldn't find the "{n}", I became worried that my Regex search isn't working. Any advice?

Thanks!
Peyton
adrian
Posts: 2395

Re: Problem with Regular Expression Search

Mon Jul 13, 2015 11:14 am

Hi,

Note that \ is a special character in regular expressions. So, if you have the option Regular expression enabled when you type \ in the Find box, a list pops up with all the expressions that start with '\'. You seem to have misinterpreted the example given for \. Searching for {n} won't yield any results, since it's a broken regular expression.

What I'm trying to do is search for this:

xtrc="/w:document/w:body[1]/w:p[299]"

where the digits in the brackets are wildcards. I did some trials and could not get it to work and then after it couldn't find the "{n}", I became worried that my Regex search isn't working. Any advice?
Search for:

Code: Select all

xtrc="/w:document/w:body\[\d+\]/w:p\[\d+\]"
where:
- \[ is the escape sequence for the special character [
- \d+ matches one or more digits

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
psbentley
Posts: 15

Re: Problem with Regular Expression Search

Mon Jul 13, 2015 4:07 pm

Perfect, thank you!! I was missing the "Wrap around" option in the find/replace screen.
psbentley
Posts: 15

Re: Problem with Regular Expression Search

Mon Aug 03, 2015 11:47 pm

Adrian, you were so helpful for my last question I hope you can help me out again!

I'm using the Word2DITA conversion tool and there are quite a few find/replaces that I need to do to get the DITA files as I want them. I'm nearly there, but the questions are giving me a bit of grief.

I want to find the correct answer and add the lcCorrectReponse2 element after my closing lcAnswerContent2.
Here's an example of what I need to search:

Code: Select all

 <lcAnswerOptionGroup2>
          <lcAnswerOption2>
            <lcAnswerContent2><pointValue value="1"/>False</lcAnswerContent2>
          </lcAnswerOption2>
          <lcAnswerOption2>
            <lcAnswerContent2>True</lcAnswerContent2>
          </lcAnswerOption2>
        </lcAnswerOptionGroup2>



And this is what I'd like to replace it with:

Code: Select all

<lcAnswerOptionGroup2>
          <lcAnswerOption2>
            <lcAnswerContent2><pointValue value="1"/>False</lcAnswerContent2><lcCorrectResponse2/>
          </lcAnswerOption2>
          <lcAnswerOption2>
            <lcAnswerContent2>True</lcAnswerContent2>
          </lcAnswerOption2>
        </lcAnswerOptionGroup2>


I'm able to use the regex search to find

Code: Select all

 <lcAnswerContent2><pointValue value="1"/>False</lcAnswerContent2>
, but when I replace it with the same expression plus lcCorrectResponse2, I get gibberish as my answer text.

This is my find:

Code: Select all

<pointValue value="1"/>\w*\s*</lcAnswerContent2>


And this is the replace I have that's not working (I have that \s* in there just in case there is an extra space after the text):

Code: Select all

<pointValue value="1"/>\w*\s*</lcAnswerContent2><lcCorrectResponse2/>


When I do this find/replace, I actually get w*s* in the place of False.

Any suggestions?

Thanks!!
Peyton
Patrik
Posts: 227
Location: Hamburg/Germany

Re: Problem with Regular Expression Search

Tue Aug 04, 2015 7:16 am

You need to use capture groups with parentheses.

Find:

Code: Select all

<pointValue value="1"/>(\w*\s*)</lcAnswerContent2>

(note the brackets around the element content)

Repalce:

Code: Select all

<pointValue value="1"/>$1</lcAnswerContent2><lcCorrectResponse2/>

(note the "$1" to refer to the content in the brackets from the regexp.)

Patrik
adrian
Posts: 2395

Re: Problem with Regular Expression Search

Tue Aug 04, 2015 9:29 am

Hi,

I would like to mention that since you just want to add something after the search match, without changing it, you can simply use $0 (the entire match) in the replace string to keep the original match unmodified.

Replace with:

Code: Select all

$0<lcCorrectResponse2/>
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
psbentley
Posts: 15

Re: Problem with Regular Expression Search

Tue Aug 04, 2015 4:39 pm

Thank you both!! This worked exactly how I wanted it to!

Return to “General XML Questions”

Who is online

Users browsing this forum: No registered users and 0 guests