Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notepad++ Search for and replace Underscore Characters in "GUIDs"

A colleague has written some C# code that outputs GUIDs to a CSV file. The code has been running for a while but it has been discovered that the GUIDs contain underscore characters, instead of hyphens :-(

There are several files which have been produced already and rather than regenerate these, I'm thinking that we could use the Search and Replace facility in Notepad++ to search across the files for "GUIDs" in this format: {89695C16_C0FF_4E7C_9BB2_8B50FAC9D371} and replace it with a properly formatted GUID like this: {89695C16-C0FF-4E7C-9BB2-8B50FAC9D371}.

I have a RegEx to find the offending GUIDs (probably not very efficient):

(([A-Z]|[0-9]){8}_)(([A-Z]|[0-9]){4})_(([A-Z]|[0-9]){4})_(([A-Z]|[0-9]){4}_(([A-Z]|[0-9]){12}))

but I don't know what RegEx to use to replace the underscores with. Does anybody know how to do this?

like image 659
lionelblak Avatar asked Dec 05 '25 17:12

lionelblak


2 Answers

You can match the pattern with 5 capture groups where you would match the underscores in between.

Then you can use the capture groups in the replacement with $1-$2-$3-$4-$5

{\K([A-Z0-9]{8})_([A-Z0-9]{4})_([A-Z0-9]{4})_([A-Z0-9]{4})_([A-Z0-9]{12})(?=})
  • { Match {
  • \K Clear the match buffer (forget what is matched so far)
  • ([A-Z0-9]{8})_ Capture group 1, match 8 times a char A-Z0-9
  • ([A-Z0-9]{4})_ Capture 4 times a char A-Z0-9 in group 2
  • ([A-Z0-9]{4})_ Same for group 3
  • ([A-Z0-9]{4})_ Same for group 4
  • ([A-Z0-9]{12}) Capture 12 times a char A-Z0-9 in group 5
  • (?=}) Positive lookahead, assert } to the right

Regex demo

If the pattern should also match without matching the curly's { and } you can append word boundaries

\b([A-Z0-9]{8})_([A-Z0-9]{4})_([A-Z0-9]{4})_([A-Z0-9]{4})_([A-Z0-9]{12})\b

Regex demo

enter image description here

like image 167
The fourth bird Avatar answered Dec 08 '25 08:12

The fourth bird


You can use the following solution:

Find What: (?:\G(?!\A)|{(?=[a-f\d]{8}(?:_[a-f\d]{4}){4}[a-f\d]{8}\}))[a-f\d]*\K_
Replace with: -
Match case: OFF

See the settings and demo:

enter image description here

See the regex demo online. Details:

  • (?:\G(?!\A)|{(?=[a-f\d]{8}(?:_[a-f\d]{4}){4}[a-f\d]{8}\})) - either the end of the previous match or a { char immediately followed with eight alphanumeric chars, four repetitions of an underscore and then four alphanumeric chars and then eight alphanumeric chars and a } char
  • [a-f\d]* - zero or more alphanumeric chars
  • \K - match reset operator that discards the text matched so far from the overall match memory buffer
  • _ - an underscore.
like image 30
Wiktor Stribiżew Avatar answered Dec 08 '25 09:12

Wiktor Stribiżew