Erik Steiner
Erik Steiner

Reputation: 601

regex: Replace a character in a match. No matter what position it has

I have an ASCII table that looks like this:

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| |NUMBR |IDENT     |YEAR |STS  |WHES |APA  |TAMS |AMOUNT          |ANOTHERAM       |DESCIB                                             |ACCO       |NUM         |ID          |
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| |99    |5471140100|2174 |002  |31   |S    |T    |         245,42 |         245,42 |*SOMEING INFORMATION 084112-378515|What. Estimation|000038780  |            |0001038780  |
| |99    |5471140100|2174 |002  |31   |S    |T    |         245,42 |         245,42 |*SOMEING INFORMATION|084112-378515-What. Estimation|000038780  |            |0001038780  |
| |99    |5471140100|2174 |002  |31   |S    |T    |         245,42 |         245,42 |*SOMEING|INFORMATION 084112-378515-What. Estimation|000038780  |            |0001038780  |

My problem is, that in the column 'DESCIB' there are sometimes pipes that are handled like delimiters (if I import this file in python), but they aren't.

I want to replace them with a blank space, but my problem is that I don't know the exact position of the "|". I only know that the column 'DESCIB' is 51 characters long.

I tried regex in Notepad++, but I have no idea how to do it.

The final result should look like this:

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| |NUMBR |IDENT     |YEAR |STS  |WHES |APA  |TAMS |AMOUNT          |ANOTHERAM       |DESCIB                                             |ACCO       |NUM         |ID          |
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| |99    |5471140100|2174 |002  |31   |S    |T    |         245,42 |         245,42 |*SOMEING INFORMATION 084112-378515 What. Estimation|000038780  |            |0001038780  |
| |99    |5471140100|2174 |002  |31   |S    |T    |         245,42 |         245,42 |*SOMEING INFORMATION 084112-378515-What. Estimation|000038780  |            |0001038780  |
| |99    |5471140100|2174 |002  |31   |S    |T    |         245,42 |         245,42 |*SOMEING INFORMATION 084112-378515-What. Estimation|000038780  |            |0001038780  |

Thank you in advance.

@EDIT: I tried this at first, but the problem is that I have to know the position of the "|":

(\*.{33})\|(.{15}\|)

Then I tried something like this: (\*.{50})(?![|]) The idea behind it is: Look for a string that starts with an "*" and has another 50 characters. In that match replace any pipes "|". However, that is not the correct usage, but I don't know how to do it.

Upvotes: 2

Views: 284

Answers (1)

Toto
Toto

Reputation: 91373

You can do that with Notepad++

Assuming that fields are fixed length

  • Ctrl+H
  • Find what: (?:^.{85}\K|\G)(.*?)\|(?=.{39,})
  • Replace with: $1
  • check Wrap around
  • check Regular expression
  • DO NOT CHECK . matches newline
  • Replace all

Explanation:

(?:         : start non capture group
  ^         : beginning of line
  .{85}     : 85 any charcater but newline
  \K        : forget all we have seen until this position
 |          : OR
  \G        : continue searching from position of last  match
)           : end group
(.*?)       : group 1, 0 or more any character, not greedy
\|          : a pipe
(?=.{39,})  : positive lookahead, at least 39 character

Replacement:

$1          : content of group 1, followed by a space

Result for given example:

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| |NUMBR |IDENT     |YEAR |STS  |WHES |APA  |TAMS |AMOUNT          |ANOTHERAM       |DESCIB                                             |ACCO       |NUM         |ID          |
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| |99    |5471140100|2174 |002  |31   |S    |T    |         245,42 |         245,42 |*SOMEING INFORMATION 084112-378515 What. Estimation|000038780  |            |0001038780  |
| |99    |5471140100|2174 |002  |31   |S    |T    |         245,42 |         245,42 |*SOMEING INFORMATION 084112-378515-What. Estimation|000038780  |            |0001038780  |
| |99    |5471140100|2174 |002  |31   |S    |T    |         245,42 |         245,42 |*SOMEING INFORMATION 084112-378515-What. Estimation|000038780  |            |0001038780  |

Upvotes: 1

Related Questions