I have a complicate string split, I need to remove the comments, spaces, and keep all the numbers but change all string into character. If the -
sign is at the start and followed by a number, treat it as a negative number rather than a operator
the comment has the style of ?<space>comments<space>?
(the comments is a place holder)
Input :
-122+2 ? comments ?sa b
-122+2 ? blabla ?sa b
output :
["-122","+","2","?","s","a","b"]
(all string into character and no space, no comments)
\s*\?\s*\w+\s*(?=\?)
with""
. You can chainString#replaceAll
to remove any remaining whitespace. Note that?=
means positive lookahead and here it means\s*\?\s*\w+\s*
followed by a?
. I hope you already know that\s
specifies whitespace and\w
specifies a word character.^-\d+|\d+|\D
which means either negative integer in the beginning (i.e.^-\d+
) or digits (i.e.\d+
) or a non-digit (\D
).Demo:
Output: