3

I have data like this:

~10~682423~15~Test Data~10~68276127~15~More Data~10~6813~15~Also Data~

I'm trying to use Notepad++ to find and replace the values within tag 10 (682423, 68276127, 6813) with zeroes. I thought the syntax below would work, but it selects the first occurrence of the text I want and the rest of the line, instead of just the text I want (~10~682423~, for example). I also tried dozens of variations from searching online, but they also either did the same thing or wouldn't return any results.

~10~.*~
Jawann
  • 53
  • 1
  • 9

2 Answers2

2

You can use: (?<=~10~)\d+(?=~) and replace with 0. This uses lookarounds to check that ~10~ precedes the digit sequence and the (?=~) ensures a ~ follows the digit sequence. If any character could be after the ~10~ field, use (?<=~10~)[^~]+(?=~).

The problem with ~10~.*~ is that the * is greedy, so it just slurps away matching any character and ~.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • 1
    This works perfectly, and even more elegantly than I expected. Your explanation about why my strategy didn't work is very helpful. Goes to show I have a lot to learn about regex. I selected the other answer because that's the one I used, but this one will help improve my skills more in the long-run. Thanks! – Jawann May 17 '20 at 22:44
1

Use

\b10~\d+

Replace with 10~0. See proof. \b10~ will capture 10 as entire number (no match in 210 is allowed) and \d+ will match one or more digits.

Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37
  • 2
    @CarySwoveland I remove capture group, I took your advice. – Ryszard Czech May 17 '20 at 21:37
  • What was life before Stack Overflow? This worked almost perfectly for me. The only change was to include another tilde at the beginning to ensure I only pick up tag `10`. Without the leading tilde, any fields that end with the number 10 will get picked up. This was the end result: `~\b10~\d+`. Thanks! – Jawann May 17 '20 at 22:40
  • 2
    @Jawann `~\b10~\d+` is the same as `~10~\d+` since `~` is not a word char. – Wiktor Stribiżew May 18 '20 at 00:44