XiCynx
XiCynx

Reputation: 15

Looking for Advanced RegEx Replacement Formula

I'm wanting to do a find/replace code. I'm using NotePad++ but can use pretty much anything else if it opens possibilities.

This is what I am currently working with:

Asus Zenfone 4
<DeviceId>
<AndroidBoardName>EeePad
<AndroidBootloader>unknown
<DeviceBrand>asus
<DeviceModel>WW_ZenFone
<DeviceModelIdentifier>LRX21V
<DeviceModelBoot>redhookbay
<HardwareManufacturer>asus
<HardwareModel>ASUS_T00I
<FirmwareBrand>ASUS_T00I
<FirmwareTags>release-keys
<FirmwareType>user
<FirmwareFingerprint>asus/WW_ZenFone/ASUS_T00I:5.0/LRX21V/WW_ZenFone-V7.4.4-20150831:user/release-keys

What I am needing to accomplish is to make it look like the following:

Asus Zenfone 4
{
DeviceId = "",
AndroidBoardName = "EeePad",
AndroidBootloader = "unknown",
DeviceBrand = "asus",
DeviceModel = "WW_ZenFone",
DeviceModelIdentifier = "LRX21V",
DeviceModelBoot = "redhookbay",
HardwareManufacturer = "asus",
HardwareModel = "ASUS_T00I",
FirmwareBrand = "ASUS_T00I",
FirmwareTags = "release-keys",
FirmwareType = "user",
FirmwareFingerprint = "asus/WW_ZenFone/ASUS_T00I:5.0/LRX21V/WW_ZenFone-V7.4.4-20150831:user/release-keys",
},

I was hoping this was possible using RegEx and it's advanced find/replace methods. Here is what I believe all needs to be done:

  1. Add a line break after the Phone Name and insert a { followed by another line break
  2. Find the opening < and remove it
  3. Find the closing > and replace it with a [space]=[space]
  4. Ignore the text between the opening and closing <>
  5. Wrap quotes around the word after the closing >
  6. Add a comma after the last quote
  7. Add a line break and insert },

If I am in over my head, please just let me know and I'll get to doing the manual labor of just copy/pasting everything one by one.

Upvotes: 1

Views: 81

Answers (1)

Sebastian Proske
Sebastian Proske

Reputation: 8413

This is possible to do in Notepad++ using Replace with activated Regular expression.

You can then search for (?:\G(?<!^)|(.+))\R<([^>]*)>(.*+)((?!\R<))? and replace with (?{1}$1\r\n\{:)\r\n$2 = "$3",(?{4}\r\n\},:).

Search pattern breakdown:

  • (?:\G(?<!^)|(.+)) - Matches the end of the previous match, if it is not the start of a line (\G matches the start of the string in the first run) or matches any character (except newline), one or more times and stores it into group one.
  • \R<([^>]*)>(.*+) - Matches a newline followed by <, then any amount of characters that are no > (stored into group 2), then > and then any amount of characters till the end of the line (stored into group 3)
  • ((?!\R<))? - Stores an empty group 4, if the next line is not starting with <

Replace pattern breakdown:

  • (?{1}$1\r\n\{:) - Conditional replace: if group 1 is present, then take the content of group 1, followed by a newline (you might want to use \n instead of \r\n depending on your systems default linebreak)
  • \r\n$2 = "$3", - add a newline, followed by the content of group 2, then space = space, finally the content of group 3, surrounded by "
  • (?{4}\r\n\},:) - Conditional replace: if group 4 is present, add a newline and },

Upvotes: 1

Related Questions