Justin
Justin

Reputation: 9724

How to replace whitespace with notepad++ between two characters

I have question how to replace whitespace with _ using notepad++ regex between [] characters

Example :

sl.[Posting date]                       AS TIME, 
'0000-00-00'                            AS edate, 
sl.[Document No_], 
[Original Currency Factor]

Result

sl.[Posting_date]                       AS TIME, 
'0000-00-00'                            AS edate, 
sl.[Document_No_], 
[Original_Currency_Factor]

Upvotes: 9

Views: 73099

Answers (3)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627537

To replace chunks of whitespace between square brackets, you can use

(?:\G(?!^(?<!.))|\[)[^][\s]*\K\s+(?=[^][]*])

And a bonus same solution for replacing whitespaces in between round brackets/parentheses:

(?:\G(?!^(?<!.))|\()[^()\s]*\K\s+(?=[^()]*\))

Replace with _ or any other char you need. NOTE: . matches newline option must be ENABLED. Or, prepend the regex with (?s), (?s)(?:\G(?!^(?<!.))|\[)[^][\s]*\K\s+(?=[^][]*]).

Details:

  • (?:\G(?!^(?<!.))|\[) - end of the previous successful match (see \G(?!^(?<!.)), \G matches start of a file or end of previous match, that is why (?!^(?<!.)) negative lookahead is added to exclude the position at the start of a file, it makes sure the current position is not a start of a line that has no more chars immedately on the left) or (|) a [ char (\[)
  • [^][\s]* - zero or more chars other than ], [ and whitespace
  • \K - match reset operator that discards all text matched so far from the overall mathc memory buffer
  • \s+ - one or more whitespaces (NOTE that if you want to replace each whitespace with _ separately, remove +)
  • (?=[^][]*]) - a positive lookahead that requires zero or more chars other than [ and ] and then ] immediately to the right of the current location.

See the demo screenshot & settings:

enter image description here

Upvotes: 1

Nguyễn Minh Vũ
Nguyễn Minh Vũ

Reputation: 440

Use

Regex Replace

(\[[^ ]*) +(.*\])

with

$1_$2

if you want to replace multiple space character with a single _ OR

Regex Replace

(\[[^ ]*) (.*\])

with

$1_$2

if you want to replace each space character with a single _

Upvotes: 0

antyrat
antyrat

Reputation: 27765

Find what: [.+(\s)+.+]

Replace with: _

Also don't forget to select Regular expression radio button in Search mode section.

Update:

Ok, I have one solution but it's dirty:

You need to perform several replacements to do that.

Find what: (\[.*)\s(.*\])

Replace with: \1_\2

Repeat using Replace all until there will be no occurrences.

Upvotes: 23

Related Questions