0

I have some code and I want to delete the first occurance of '}' (or the 2nd, doens't matter) after a certain regexpr, using sed or whatever. I don't understand how to point my regexpr to this.

Code I work with looks like this:

lore ipsum ^{someWord}} lore ipsum

The regexpr I found so far looks like this:

\^{[a-zA-z]*}
Landorlin
  • 3
  • 1
  • Use `sed 's/\^{[^{}]*}//g'` if you need a `lore ipsum } lore ipsum` as a result. Or, `sed 's/\^{\{1,\}[^{}]*}\{1,\}//g'` to remove all the `{` at the start and `}`s at the end. See [this demo](https://ideone.com/khLV3s). – Wiktor Stribiżew Apr 30 '20 at 16:45
  • Capture the first part including the first `}` and match the second. Replace with group 1 `echo "lore ipsum ^{someWord}} lore ipsum" | sed -En 's/(\^{[a-zA-Z]*})}/\1/p'` – The fourth bird Apr 30 '20 at 16:50

1 Answers1

0

You could capture including the first } in a group and match the second } to be removed. In the replacement use the first capturing group.

Pattern

(\^\{[a-zA-Z]*\})\}

Regex demo | Sed demo

Example

sed -E 's/(\^\{[a-zA-Z]*\})\}/\1/' <<< "lore ipsum ^{someWord}} lore ipsum"

Output

lore ipsum ^{someWord} lore ipsum

Note that [a-zA-z] matches more than [a-zA-Z]

The fourth bird
  • 154,723
  • 16
  • 55
  • 70