27

I have a file with a some comma separated names and some comma separated account numbers.
Names will always be something like Dow, John and numbers like 012394,19862.

Using Notepad++'s "Regex Find" feature, I'd like to replace commas between numbers with pipes |.

Basically :

turn:  Dow,John      into:  Dow,John
       12345,09876          12345|09876
       13568,08642          13568|08642

I've been using [0-9], to find the commas, but I can't get it to properly leave the number's last digit and replace just the comma.

Any ideas?

Toto
  • 89,455
  • 62
  • 89
  • 125
JToland
  • 3,630
  • 12
  • 49
  • 70

4 Answers4

36

Search for ([0-9]), and replace it with \1|. Does that work?

Joshua Dwire
  • 5,415
  • 5
  • 29
  • 50
  • 3
    Ah, this seems to do it, thanks! I was trying `[0-9]` and replacing it with `\1|', which was NOT working, but adding the parenthesis as you did here works perfectly. Thanks again! – JToland Oct 31 '12 at 18:33
  • 1
    Why do we need the parens? – Patrick Apr 01 '16 at 08:54
  • 1
    @Patrick In regex, the parentheses create a capture group telling the parser to capture the parts within the group so that it can be used for replacements. – Joshua Dwire Apr 01 '16 at 13:29
  • 1
    This also seems to work for TextPad. Ex: Find ([0-9]),([a-z]) and replace \1|\2 – majestzim Nov 21 '16 at 20:29
  • 1
    Parenthesis is called "Grouped Backreference" in Regular Expression terminology. ()()() - in search can be accessed in replace as \1 \2 \3 :) – Chandraprakash Sep 12 '19 at 10:47
15

use this regex

(\d),(\d)

and replace it with

$1|$2

OR

\1|\2
Anirudha
  • 32,393
  • 7
  • 68
  • 89
  • @Blender that would make the same the same thing – Anirudha Oct 31 '12 at 18:28
  • This actually replaced "$1|$2" in-line into the text -- like it wasn't parsing $1 as anything but the intended string I wanted. – JToland Oct 31 '12 at 18:32
  • @Fake.It.Til.U.Make.It Ah, now that works. Thanks for this answer, though I've already marked the other as "the answer" since I used that one and had it working for me first. – JToland Oct 31 '12 at 18:38
3

(?<=\d), should work. Oddly enough, this only works if I use replace all, but not if I use replace single. As an alternative, you can use (\d), and replace with $1|

Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
  • I don't believe Notepad++ supports the `Lookahead` and `Lookbehind` assertions. – Nick Oct 31 '12 at 22:32
  • @Nick actually, it does. I have NP++ and this works (finds matches), but only replaces them if I use replace all. If I try to replace them case by case, it leaves them unchanged. – Asad Saeeduddin Nov 01 '12 at 05:49
  • I believe you, I had only heard that. I haven't used NP++ for regex before. – Nick Nov 01 '12 at 06:16
  • I'm surprised there is a difference between Replace and Replace All - that's definitely worth remembering. – Jerry Jeremiah Nov 29 '20 at 23:52
2

General thoughts about replacing only part of a match

In order to replace a part of a match, you need to either 1) use capturing groups in the regex pattern and backreferences to the kept group values in the replacement pattern, or 2) lookarounds, or 3) a \K operator to discard left-hand context.

So, if you have a string like a = 10, and you want to replace the number after a = with, say, 500, you can

  • find (a =)\d+ and replace with \1500 / ${1}500 (if you use $n backreference syntax and it is followed with a digit, you should wrap it with braces)
  • find (?<=a =)\d+ and replace with 500 (since (?<=...) is a non-consuming positive lookbehind pattern and the text it matches is not added to the match value, and hence is not replaced)
  • find a =\K\d+ and replace with 500 (where \K makes the regex engine "forget" the text is has matched up to the \K position, making it similar to the lookbehind solution, but allowing any quantifiers, e.g. a\h*=\K\d+ will match a = even if there are any zero or more horizontal whitespaces between a and =).

Current problem solution

In order to replace any comma in between two digits, you should use lookarounds:

Find What: (?<=\d),(?=\d)
Replace With: |

Details:

  • (?<=\d) - a positive lookbehind that requires a digit immediately to the left of the current location
  • , - a comma
  • (?=\d) - a positive lookahead that requires a digit immediately to the right of the current location.

See the demo screenshot with settings:

enter image description here

See the regex demo.

Variations:

Find What: (\d),(?=\d)
Replace With: \1|

Find What: \d\K,(?=\d)
Replace With: |

Note: if there are comma-separated single digits, e.g. 1,2,3,4 you can't use (\d),(\d) since this will only match odd occurrences (see what I mean).

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563