2

I'm missing something with this regular expression find/replace attempt. I have the following format:

word | word | word

I would like to first replace every word with "word" to produce

"word" | "word" | "word"

and then subsequently every [space]| with ,, finally producing

"word", "word", "word"

Obviously I could just do this with two simple find(f)/replace(r) commands ( f:([a-z]*\>)r:"$1"; f:[space]|r:,), but is there a way to do all of this at once?

I've tried lots of different ideas, but they all failed. The most successful was finding ([a-z]*\>)(( \|)|\R) and replacing with "$1",, which only ever got me a "word", "word", word format. The solution is probably either much more complicated or much simpler than I'm trying, but I'm stumped. Thanks!

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
ganondorc
  • 29
  • 7
  • I am also stumped. I don't even think we could phrase this replacement in an app language using a single regex, because the replacements are multiple things. Honestly, I like the first approach you described, and I don't see anything wrong with that. +1 to an otherwise good question. – Tim Biegeleisen Nov 27 '18 at 14:40

1 Answers1

2

You may use

(\w+)|\s*\|

and replace with (?1"$1":,).

Details

  • (\w+) - Group 1: one or more word chars
  • | - or
  • \s*\| - 0+ whitespaces and then a | char.
  • (?1"$1":,) - a conditional replacement pattern that replaces with " + Group 1 contents + " if Group 1 matches, else, replaces with ,.

enter image description here

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Thank you so much - what kind of documentation are you consulting/did you once consult to learn this? The notepad++ documentation is confusingly worded, and I didn't see this cool conditional pattern explained very well, if it was explained at all. Also, very clear formatting and details, very much appreciated! – ganondorc Nov 27 '18 at 16:09
  • 1
    @MichaelFrake See [How to use conditionals when replacing in Notepad++ via regex](https://stackoverflow.com/a/37161309/3832970). Here is the relevant [Boost documentation](https://www.boost.org/doc/libs/1_68_0/libs/regex/doc/html/boost_regex/format/boost_format_syntax.html). – Wiktor Stribiżew Nov 27 '18 at 17:16