0

Trying to match everything except the text in the brackets, excluding the brackets themselves. For example I am trying to match everything but {PRODUCTTYPE} and {PRODUCTCODE} in the text below:

^FT14,155^A0N,75,74^FD{PRODUCTTYPE}^FS{PRODUCTCODE}^A0N,75,74^FD

Closest I got was with this regex:

.*(?=\{)|(?=\}).*

The problems are 1. this only works for one of the occurrences and 2. it includes the closing bracket, despite the documentation stating that it excludes the matched group of the result. Ideally using lookarounds, but without also ok. What am I missing?

EDIT I am using a text editor (sublime text), not a programming language. I need the regex to do all the work without programming help. I would like to be able to keep what is underlined in red, so when I hit delete, only what is outside the brackets is deleted:

enter image description here

I know in practice there are other ways to achieve this but I would like to know how to do this with regex. Notepad++ is also ok.

evilmandarine
  • 4,241
  • 4
  • 17
  • 40
  • You could for example use a capturing group `{.*?}|([^{}]+)` https://regex101.com/r/coftUI/1 Are the `{` and `}` chars accepted in the other parts as well? – The fourth bird Apr 27 '20 at 11:21
  • 1
    Everything except a pattern can be easily done using *a regex split method* with your regex. So, use `split()` with `{[^{}]*}` regex. You will get an array of `^FT14,155^A0N,75,74^FD`, `^FS`, and `^A0N,75,74^FD` that you may join later if you need. – Wiktor Stribiżew Apr 27 '20 at 11:24
  • @Thefourthbird yes they are. Is it possible to get only the groups I want in the result? I am working in a text editor, not programming. – evilmandarine Apr 27 '20 at 12:10
  • @WiktorStribiżew This question is not a duplicate of that other question, which I read and did not allow to solve the issue. – evilmandarine Apr 27 '20 at 12:11
  • Sorry, but if you use SublimeText, what is the expected result? Also, for a task like this, Notepad++ is much better. Do you consider using NPP? – Wiktor Stribiżew Apr 27 '20 at 16:01
  • @WiktorStribiżew Added more explanation. Yes, NPP would also be ok. – evilmandarine Apr 27 '20 at 16:16
  • @supafly I understand it that you need to remove all strings inside `{...}` - just use `\{[^{}]*\}` then. Right? – Wiktor Stribiżew Apr 27 '20 at 16:20
  • @WiktorStribiżew Absolutely right, my bad, I want the opposite. I want hit delete and only stuff between {...} remain in the text, the rest is deleted, sorry. – evilmandarine Apr 27 '20 at 16:24
  • Ok, there is a good solution in Notepad++, but I think you may also use Sublime with `(?:\{([^{}]*)\}|(?:(?!\{[^{}]*\}).)+)` -> `$1\n`. – Wiktor Stribiżew Apr 27 '20 at 16:26
  • I changed the dupe reason, you may use `(?s)\{([^{}]+)\}|.` => `(?{1}$1\n:)` i Notepad++. – Wiktor Stribiżew Apr 27 '20 at 16:39
  • @WiktorStribiżew Ok first one works on both, second is better but only works in NPP. This is what I needed. If you post as an answer I'll accept it, thank you! – evilmandarine Apr 27 '20 at 16:47
  • I suggest keeping this one a duplicate as a flagpost for others. – Wiktor Stribiżew Apr 27 '20 at 18:33

0 Answers0