1

I want to preface by saying I am a novice at regex, and I've spent a considerable amount of time trying to solve this myself using tutorials, online docs, etc. I have also gone through the suggested answers here.

Now here is my problem: I have 267 lines like this, and each county is different.

<SimpleData name="NAME">Angelina</SimpleData>

What I need to do is to replace NAME with COUNTY and keep the rest the same including the proper county name:

<SimpleData name="COUNTY">Angelina</SimpleData>

I used the following Find to find all the lines that I wanted to change, and was successful.

<SimpleData name="NAME">[\S\s\n]*?</SimpleData>

It's probably not the best way to do this, but it worked.

I hope I've explained this so it can be understood. Thanks, Paul

Odaat
  • 95
  • 1
  • 6
  • 1
    Do you just wanna replace "NAME" by "COUNTY" ? If so, why do you want to use regex a simple "replace" will do the job. – Unkle Benz Feb 06 '19 at 16:07
  • Search for `([\S\s\n]*?)` and replace with `$1COUNTY$2` – Wiktor Stribiżew Feb 06 '19 at 16:59
  • @UnkleBenz Yes, that is the simple solution, but I have other strings where I will also need the solution I'm asking for. Thanks – Odaat Feb 06 '19 at 18:44
  • @WiktorStribiżew Your solution works a treat! Thank you. I'm not sure how to mark a simple comment as the correct answer? – Odaat Feb 06 '19 at 18:46
  • I posted it with some more explanation and links to the docs. – Wiktor Stribiżew Feb 06 '19 at 19:16
  • 1
    VSCode is a *text editor*, there is no access to code, thus, the [How to change XML Attribute](https://stackoverflow.com/questions/367730/how-to-change-xml-attribute) cannot be used to close this post as a duplicate. – Wiktor Stribiżew Feb 07 '19 at 14:49

2 Answers2

2

You need to use capturing groups with backreferences in the replacement field:

Find What: (<SimpleData name=")NAME(">[\S\s\n]*?</SimpleData>)
Replace With: $1COUNTY$2

See the regex demo

As per regular-expressions.info:

Besides grouping part of a regular expression together, parentheses also create a numbered capturing group. It stores the part of the string matched by the part of the regular expression inside the parentheses.

If your regular expression has named or numbered capturing groups, then you can reinsert the text matched by any of those capturing groups in the replacement text. Your replacement text can reference as many groups as you like, and can even reference the same group more than once. This makes it possible to rearrange the text matched by a regular expression in many different ways.

Note that, in VSCode, you can't use named groups.

Community
  • 1
  • 1
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

You really don't want to be using regex for this job. Learn XSLT.

Any attempt to do this using regular expressions will either match things it shouldn't, or will fail to match things that it should. That's not because you're lacking regex skills, it's because of the computer science theory: XML's grammar is defined recursively, and regular expressions can't handle recursively-defined grammars.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164