How To Delete Characters Inside Brackets [ ]

Basically, I have the following string:

I want the script to remove only this part:

So that I am left with:

I was provided with the following Applescript which does the job:

set theString to "@junior_cat23, [ID]-[E9B-Z8X-H1V-722]"
do shell script "sed -E -e 's|\\[.*||' -e 's|,||g' -e 's|^[ ]+||' -e 's|[ ]+$||'<<<" & theString's quoted form

But the problem is that it only removes items that are inside the brackets when they are located on the right hand side of the string.

If I have the following string:

It will delete the entire string.

set theString to "[ID]-[E9B-Z8X-H1V-722] this is a test @junior_cat23 @james_cat24"
do shell script "sed -E -e 's|\\[.*||' -e 's|,||g' -e 's|^[ ]+||' -e 's|[ ]+$||'<<<" & theString's quoted form

If I have the following:

It will only return:

set theString to "this is a test [ID]-[E9B-Z8X-H1V-722] @junior_cat23 @james_cat24"
do shell script "sed -E -e 's|\\[.*||' -e 's|,||g' -e 's|^[ ]+||' -e 's|[ ]+$||'<<<" & theString's quoted form

I just want to remove the characters in side of the brackets no matter where they are located in the string (wether they are on the left, right or middle) and keep the others.

I did find a shell script which works in macOS Terminal but I am having issues integrating it into an Applescript

I tried adapting it to my script editor with the “do shell script” command and I get the following error:

Here is the script:

set theString to "this is a test [ID]-[E9B-Z8X-H1V-722] @junior_cat23 @james_cat24"
do shell script "sed -E -e 's/\[[^]]*\]//g' |sed -e 's|-||g' |sed -e 's/,/ /g'" & theString's quoted form

Any help is appreciated. Thanks!

Here’s an alternative approach:

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

set theText to "@junior_cat23, [ID]-[E9B-Z8X-H1V-722]
this is a test [ID]-[E9B-Z8X-H1V-722] @junior_cat23 @james_cat24"
set theText to current application's NSString's stringWithString:theText
return (theText's stringByReplacingOccurrencesOfString:",? \\[[^]]+?]-\\[[^]]+?]" withString:"" options:(current application's NSRegularExpressionSearch) range:{0, theText's |length|()}) as text

Thank you so much! I see it does get the result I want. But I am still curious to know why the shell script is not working with in the Script Editor. I see it works flawlessly in the macOS Terminal. Any ideas as to what the problem might be?

Works fine in Terminal:

Does not work in Applescript Editor:

set theString to "this is a test [ID]-[E9B-Z8X-H1V-722] @junior_cat23 @james_cat24"
do shell script "sed -E -e 's/\[[^]]*\]//g' |sed -e 's|-||g' |sed -e 's/,/ /g'" & theString's quoted form

That sort of thing is usually a quoting or escaping problem.

This works with all three of your examples and a pathological one:

set theString to "@junior_cat23, [ID]-[E9B-Z8X-H1V-722]
[ID]-[E9B-Z8X-H1V-722] this is a test @junior_cat23 @james_cat24
this is a test [ID]-[E9B-Z8X-H1V-722] @junior_cat23 @james_cat24
[ID]-[E9B-Z8X-H1V-722] this is a test [ID]-[E9B-Z8X-H1V-722][ID]-[E9B-Z8X-H1V-722] @junior_cat23, [ID]-[E9B-Z8X-H1V-722] @james_cat24[ID]-[E9B-Z8X-H1V-722]"

do shell script "sed -E 's/^\\[[^]]+\\]-\\[[^]]+\\] *|,? *\\[[^]]+\\]-\\[[^]]+\\]//g' <<<" & theString's quoted form

Perfect. I guess will use one of the two solutions posted instead of trying to fix the error I am getting with my original shell script. Thanks.