How to remove only the characters inside brackets [ ] from a string?

Solution 1:

Updated Answer

This is the most straight forward method to accomplish the goal:

Example AppleScript code:

set theName to "@junior_cat23, [ID]-[E9B-Z8X-H1V-722]"

set theName to ¬
    characters 1 thru ¬
        ((offset of "," in theName) - 1) ¬
            of theName as string

Result:

@junior_cat23

To address the comment:

What if I have multiple names, for example "@junior_cat23 @james_cat24 [ID]-[E9B-Z8X-H1V-722]" and I want to keep both of them while the string does not have commas?

Example AppleScript code:

set theName to "@junior_cat23 @james_cat24 [ID]-[E9B-Z8X-H1V-722]"

set theName to ¬
    characters 1 thru ¬
        ((offset of "[" in theName) - 2) ¬
            of theName as string

Result:

@junior_cat23 @james_cat24

Original Answer

One of the reasons you were not getting returned what you wanted is in some cases AppleScript is case insensitive and wrapping the -- Remove Characters code in a considering case block handles some of the issues, but not all. Additionally, since you have numeric characters in both what you want retained and not, then as currently coded it removes all numeric characters.

While your current code could be reworked, I'm not inclined to do that as from both deleted comments and still existing comments you've left out vital information in the OP. That said, I'll assume for the moment that all target strings will have either , [ ... ] or [ ... ] that needs to be removed and the following sed command handles the example value of theName variable in your OP and in the comments.

So whether you have:

set theName to "@junior_cat23, [ID]-[E9B-Z8X-H1V-722]"

Or:

set theName to "@junior_cat23 @james_cat24 [ID]-[E9B-Z8X-H1V-722]"

Then:

do shell script "sed -E -e 's|\\[.*||' -e 's|,||g' -e 's|^[ ]+||' -e 's|[ ]+$||'<<<" & theName's quoted form

      • Hint: Mouse over and horizontal scroll to see full code.

Returns:

@junior_cat23

Or:

@junior_cat23 @james_cat24

Without any leading/trailing spaces.


Understanding the do shell script and sed commands:

  • do shell script -- Executes a shell script using the sh shell.
  • sed -- Stream Editor.
  • -E -- Interpret regular expressions as extended (modern) regular expressions rather than basic regular expressions (BRE’s). The re_format(7) manual page fully describes both formats.
  • −e command -- Append the editing commands specified by the command argument to the list of commands.
  • 's|\\[.*||' -- Removes everything from the first [ character to the end of the line.
  • 's|,||g' -- Removes all commas, if they exist.
  • 's|^[ ]+||' -- Removes all leading spaces, if they exist.
  • 's|[ ]+$||' -- Removes all trailing spaces, if they exist.
  • <<< -- Here Strings - A variant of here documents, the format is: [n]<<< word - The word undergoes brace expansion, tilde expansion, parameter and variable expansion, command substitution, arithmetic expansion, and quote removal. Pathname expansion and word splitting are not performed. The result is supplied as a single string, with a newline appended, to the command on its standard input (or file descriptor n if n is specified).
  • & theName's quoted form -- Appends the single quoted value of the variable theName to the end of the command line of the sed command in the do shell script command so as not to expand any shell special characters in the value of the variable theName, if any exists.