1

A file has hundreds of lines of text similar to the one below:

<Random Variable1="test" Description="this is a random description" number="12345" />

The value "<Random" at the start and the value "/>" at the end is the same on all lines of text.

I am trying to use regular expression to replace the /> symbol at the end with > and insert new lines of text underneath. An example of what the line from above needs to be replaced to:

<Random Variable1="test" Description="this is a random description" number="12345" >
         <Operating>
          <Operating>All</Operating>
        </Operating>
      </Random>

I have been testing with regular expressions but have not been successful, regular expressions I have tried to use:

To add in the new lines:

<Operating>\n<Operating>All</Operating>\n</Operating>\</Random>

To change the /> character:

<Random(.*) >

EDIT: I am using the find and replace option within Visual Studio 17

1
  • 1
    Which language/tool are you using?
    – Toto
    Sep 24, 2021 at 16:14

1 Answer 1

1

If your language/tool supports PCRE regex flavour, you can do:

  • Find: (?s)<Random\b.*?\K/> or <Random\b.*?\K/> for multilines
  • Replace: ><Operating>\n<Operating>All</Operating>\n</Operating>\n</Random>

If it doesn't, use:

  • Find: (<Random\b.*?)/>
  • Replace: $1><Operating>\n<Operating>All</Operating>\n</Operating>\n</Random>

You may have to escape the < and >

  • Find: (\<Random\b.*?)/\>
2
  • Thank you for the feedback Toto, I am using Visual Studio 17's find and replace tool. Visual Studios didn't seem to like the find regular expression as it didn't catch anything :(
    – Help
    Sep 24, 2021 at 16:23
  • Thank you, that works!
    – Help
    Sep 24, 2021 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.