0

In Notepad++, how can I replace a certain character (.) that is enclosed within two brackets [] throughout a large file?

Explanation

I have a very long code dump file (of PeopleCode) in Notepad++. At the top of each section of code, there is a tag like this:

[G3FORM.FormFactory.OnExecute]

This denotes a class package, and a class. In addition, this can be multiple layers deep:

[Y_REST.RESTQueries.ExampleClass.OnExecute]

Whenever a class is declared in the code, it doesn't use .'s. It uses :'s. E.g.:

import G3FORM:Form;

So, when I want to search for that class in the code dump, I have to change all of the .'s into :. I can do that, but it's somewhat annoying.

Example

I would like some help with Notepad++'s regex search feature for find and replace. I need to replace all of the .'s contained within these headers with :'s. E.g.:

[Y_REST.RESTQueries.ExampleClass.OnExecute]

would become

[Y_REST:RESTQueries:ExampleClass.OnExecute]

All but the last colon are replaced (and even then, if that makes it overly complicated, the last . can also be replaced).

Failed Attempts

I have been playing around with some regex to try and get this, but I am not familiar enough with it.

This doesn't work at all:

(([A-Z])\.)+

This also doesn't find anything:

\[(([A-Z])\.)+\]

I'm probably way off base. I haven't used regex enough to be familiar enough to know how to use the find and replace with regex.

Community
  • 1
  • 1
Cache Staheli
  • 2,897
  • 6
  • 30
  • 41

3 Answers3

0

Do a regular expression find/replace like this:

  • Open Replace Dialog
  • Find What: (\[[^\].]+)\.([^\]]+\])
  • Replace With: \1:\2
  • check regular expression
  • click Replace or Replace All
  • Keep Alt-A (Replace All) pressed until the message in find dialogs status bar tells you "0 occurences were replaced"
Lars Fischer
  • 7,817
  • 3
  • 24
  • 31
  • What does the `\1:\2` do? – Cache Staheli Jul 21 '16 at 20:25
  • Also, doesn't seem to catch multiple `.`'s as in the case of `[Y_DCE_FUNCLIB:DCE_Email.BATCH_EMAIL_MANAGERS.EFYFilterRejectedBatchEmail.OnExecute]` – Cache Staheli Jul 21 '16 at 20:31
  • @CacheStaheli *ReplaceAll* replaces *one* "." in *all* lines. Thus you have to keep Alt-A pressed to replace until all replacements are done. For a basic introduction to Regular expression, please refer to http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean/22944075#22944075 . The `\1` and `\2` are backreferences that are filled from the *Find What *part with the actual text matched by the first and second parentheses. pair. – Lars Fischer Jul 22 '16 at 13:53
0

Try this (?:(?!\A)\G|\[(?=[^\[\]]+\]))[^\[\].]*\K\.

Explanation

 (?:                           # Cluster start
      (?! \A )                      # Not beginning of string
      \G                            # G anchor - If matched before, start at end of last match
   |                              # or
      \[                            # Open bracket
      (?= [^\[\]]+ \] )             # Only if there is a closing bracked down stream
 )                             # Cluster end
 [^\[\].]*                     # Not brackets nor dot
 \K                            # Exclude previous in match
 \.                            # The dot, all thats left

Replace with whatever you want.
Note - for performance reasons, we limit latency by checking for closing
bracket only once per open bracket \[ (?= [^\[\]]+ \] )

0

Since the number of levels is limited, one really straightforward approach is to use several searches, one for each number of levels.

Here is a search/replace for four levels:

Find what :   \[([^.]+)\.([^.]+)\.([^.]+)\.([^\]]+)\]
Replace with: \[$1:$2:$3:$4\]

Search expression is composed of ([^.]+)\. "not dots followed by a dot" groups enclosed in square brackets.

$1..$4 mean capturing groups, i.e. what's matched by the expression in parentheses.

To perform the replacement for fewer or more levels, add remove or add \.([^\.]+) in the search box, and adjust the replace box accordingly.

Sergey Kalinichenko
  • 675,664
  • 71
  • 998
  • 1,399