How to Match Tilde-Delimited Data Using Regex

Viewed 107

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~.*~
2 Answers

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 ~.

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.

Related