2

Ok so I'm really struggling to figuring this one out. There where other post on here answering this question but non of them worked out for my use case (GREP InDesign).

Input:

meelebox meelebox meeleboxmeeleduelist meeleboxuser meelebox meelebox meelebox

--

Match:

meele meele meelemeeleduelist meeleuser meele meele meele

I got \w+(?=(box))|(?<=box)\w+ to work, but I really want an expression where I only need to write the word once, as it's an expression I use a lot and that might change over time.

I thought I figured it out with this \w+(?=(box))|(?<=\2)\w+ but the \2 doesn't seem to work.

  • Like this? [`(box)|.`](https://regex101.com/r/ekkaRN/1) will help to add some context :) – bobble bubble Sep 27 '22 at 17:26
  • With PCRE `(?(DEFINE)(?box))(?:\w+(?=(?&b))|(?&b)\K\w+)` https://regex101.com/r/b5lu98/1 What should be the match for `meleeboxbox` ? – The fourth bird Sep 27 '22 at 17:51
  • Sorry guys, I wrote the match wrong. it's suppose to Exclude the string "box" so match would be: meele meele meelemeeleduelist meeleuser meele meele meele – Christoffer Andersen Sep 27 '22 at 18:21
  • But it does give you those matches right? https://regex101.com/r/8pbUYX/1 – The fourth bird Sep 27 '22 at 18:51
  • [Some variation](https://regex101.com/r/xNxhKP/1) of @the4thbird's idea. I was playing with it again xD Funnily on my first reading I've totally overlooked that your main reason is to not use the word `box` a second time. If you added more context there might be some other solution such as [just using capuring groups](https://regex101.com/r/VlxH2K/1). – bobble bubble Sep 27 '22 at 20:25

1 Answers1

1

The problem you defined doesn't even require RegEx. It can be solved using simple string replace, where you replace occurrences of box with an empty string.

❯ echo "meelebox meelebox meeleboxmeeleduelist meeleboxuser meelebox meelebox meelebox" | sed s/box//g
meele meele meelemeeleduelist meeleuser meele meele meele
pavi2410
  • 1,220
  • 2
  • 12
  • 16