What is the most efficient or elegant method for matching brackets in a string such as:
"f @ g[h[[i[[j[2], k[[1, m[[1, n[2]]]]]]]]]] // z"
for the purpose of identifying and replacing [[ Part ]]
brackets with the single character forms?
I want to get:
With everything else intact, such as the prefix @
and postfix //
forms intact
An explanation of Mathematica syntax for those unfamiliar:
Functions use single square brackets for arguments: func[1, 2, 3]
Part indexing is done with double square brackets: list[[6]]
or with single-character Unicode double brackets: list〚6〛
My intent is to identify the matching [[ ]]
form in a string of ASCII text, and replace it with the Unicode characters 〚 〛
Ok, here is another answer, a bit shorter:
Example:
EDIT
You can also use Mathematica built-in facilities, if you want to replace double brackets specifically with the symbols you indicated:
The result would not show here properly, but it is a Unicode string with the symbols you requested.
Here is my attempt. The pasted ASCII code is pretty unreadable due to the presence of special characters so I first provide a picture of how it looks in MMA.
Basically what it does is this: Opening brackets are always uniquely identifiable as single or double. The problem lies in the closing brackets. Opening brackets always have the pattern string-of-characters-containing-no-brackets + [ or [[. It is impossible to have either a [ following a [[ or vice versa without other characters in-between (at least, not in error-free code).
So, we use this as a hook and start looking for certain pairs of matching brackets, namely the ones that don't have any other brackets in-between. Since we know the type, either "[... ]" or "[[...]]", we can replace the latter ones with the double-bracket symbols and the former one with unused characters (I use smileys). This is done so they won't play a role anymore in the next iteration of the pattern matching process.
We repeat until all brackets are processed and finally the smileys are converted to single brackets again.
You see, the explanation takes mores characters than the code does ;-).
Ascii:
Oh, and the
Whitespace
part is because in Mathematica double brackets need not be next to each other.a[ [1] ]
is just as legal as isa[[1]]
.Here's another one with pattern matching, probably similar to what Sjoerd C. de Vries does, but this one operates on a nested-list structure that is created first, procedurally:
and with these definitions then
gives