Java regex - erase characters followed by \b (backspace) -
i have string constructed user keyboard types, might contain '\b'
characters (backspaces).
i want clean string, not contain '\b'
characters, characters meant erase. instance, string:
string str = "\bhellow\b world!!!\b\b\b.";
should printed as:
hello world.
i have tried few things replaceall, , have is:
system.out.println(str.replaceall("^\b+|.\b+", ""));
which prints:
hello world!!.
single '\b'
handled fine, multiples of ignored.
so, can solve java's regex?
edit:
i have seen this answer, seem not apply java's replaceall.
maybe i'm missing verbatim string...
it can't done in 1 pass unless there practical limit on number of consecutive backspaces (which there isn't), , there guarantee (which there isn't) there no "extra" backspaces there no preceding character delete.
this job (it's 2 small lines):
while (str.contains("\b")) str = str.replaceall("^\b+|[^\b]\b", "");
this handles edge case of input "x\b\by"
has backspace @ start, should trimmed once first 1 consumes x
, leaving "y"
.
Comments
Post a Comment