How to remove characters from a matching string

notepadregex

I want to remove spaces from a list of phone numbers. The phone numbers are in the following format: "+33 x xx xx xx xx" most of the time (quotes included). Spaces can be placed differently.

In notepad++, my "Find what:" field looks like this, and effectively matches every phone number encountered: "\Q+\E[0-9 ]+". But I don't understand how to remove a part of it (the spaces).

I thought about doing it in two steps: first selecting the results of a search with the matching pattern above, and then doing a replacement with the "In selection" checkbox ticked, but I can't find how to select all search results at once.

Best Answer

The 'simple' way is to use capture groups for each group of numbers. For a fixed format, that works well. Find:

"\+33 ([0-9]) ([0-9]{2}) ([0-9]{2}) ([0-9]{2}) ([0-9]{2})"

And replace with:

"+33$1$2$3$4"

The other way is a bit more flexible, but at the same time, can be a bit confusing, since it uses the \G anchor which is not so common, with the \K anchor, which might be even less common:

(?:"\+33|\G)[^\s"]*\K 

(There is a space at the end) And replace with nothing.

regex101 demo

(?:          # Begin group
  "\+33      # Match "+33
|            # OR
  (?!^)\G    # At the end of the previous match (and exclude start of lines)
)            # End group
[^\s"]*      # Any non-space or quote characters
\K           # Reset the match
             # Match a space

Both \G and \K aren't available on some earlier versions of Notepad++. I don't remember exactly when they were introduced, but they work on v6.5.2.

Related Question