正则表达式删除新行和反斜杠

时间:2019-07-29 09:57:07

标签: java android regex

我有一个请求有效负载,我需要用下面的正则表达式删除新行'\ n':

replaceAll("\n", "")

然后它留下这样的字符串:

eK7jVPstMFlOJ\/\/PtFIOKgSODxmjpQNgm9ASwQzP0v2RJbZzVXEB5ZOKVxiILhSCng25D87K8P9oHFScbS6OsmGDIf1HMDlXmcSn0JuBFfhx36GGoLpMq3\/xH3wB1Ku5x5\/6WSiijsYit28Rf\/3ZsK2U1PCc9NVVAeoZvLsn\/skjishzVGKJv9elOgkXcoM2F3LbMlrwjw9j4lx07RbeGfq9H7+oANeLCreSZEpe7iC8+zKatT4TCrb7Q3ZW\/FlE1\/JwnOphvdcKDoQvzPrxizN9idrwidF6\/y30CkCB+aW39Bb5dB+YdXmW9eZJNt4ZlI9embs+ZwAKNgZq\/52W\/Q=="

因此,我收到错误响应:由于[Source:(String)\“ \”;中的输入结束\ n,没有内容要映射行:1,列:0]

如何删除新行和反斜杠,仅保留正斜杠

(示例/最终格式的代码应类似于正斜杠)

qRqWnruUJGoEGuBPcRRe4Td/BkesYk0KdoaNO6j7QQULZExKE8jVnFdzjJxNA2GPkazen/hcKx3+DMQ7RWupau473z6uF0iVjxcl/U6mTQbtY5tb8c/Qq64C5nYU+iplW+/e1lWyKzZed7IegNRVSABKbyyTrqTmQCghPMCY3evo/YwF0EovKw5Zw1U1BzjMizr7jhBihTsq71WE4MXW4iyhxGbnvPuzqpXBowjcOAyx433PjRDZnty5mum3TeETJMpoF20ULa7q5mnNCW6JbkZErhzGbY5thJNeYUvwhlsP3qFYNlFkO0kKax/nEfxFC6muowOOrFsqerbP0lcAHQ==

1 个答案:

答案 0 :(得分:-1)

您需要做的声音也就是替换您的反斜杠...

您可以链接呼叫,因此请尝试以下操作:

final String result = original.replaceAll("\n", "")
                              .replaceAll("\\\\", "");

或者,对于单行解决方案:

final String result = original.replaceAll("([\\\\\n])", "");