删除字符串中的某些字符,Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13229157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Removing Certain Characters inside a String, Java
提问by Christian Eric Paran
My problem here is that i want a Character remove in some parts of a String but I do not know how to restrict the removing.
我的问题是我想在字符串的某些部分删除一个字符,但我不知道如何限制删除。
Example:
例子:
A computer is a general purpose device that can be\n
programmed to carry out a finite set of\n
millions to billions of times more capable.\n
\n
In this era mechanical analog computers were used\n
for military applications.\n
1.1 Limited-function early computers\n
1.2 First general-purpose computers\n
1.3 Stored-program architecture\n
1.4 Semiconductors and\n
this here example is the content of my string, what i want to happen is to remove the \n
of lines 1 and 2 above but not to remove the \n
in line 5 onwards. How do i remove the \n
without removing the other \n
?. My Goal here is to make the string a paragraph without \n
after line. like the example the first 3 lines can be a paragraph and the next lines are in bullet form(example). what i am saying is that I do not want to remove \n
in bulleted characters.
这里的示例是我的字符串的内容,我想要发生的是删除\n
上面的第 1 行和第 2 行,但不删除第\n
5 行以后的内容。我如何删除\n
而不删除另一个\n
?. 我的目标是使字符串成为没有\n
行后的段落。像示例一样,前 3 行可以是一个段落,下一行是项目符号形式(示例)。我的意思是我不想删除\n
项目符号字符。
The real contents of the string is dynamic.
字符串的真实内容是动态的。
I have tried using String.replaceAll("\n", " ")
well clearly that would not work it will remove all the \n
i have thought of using Regex
to determine what is Alphanumberic but it would remove some letters after \n
我已经尝试过String.replaceAll("\n", " ")
清楚地使用它是行不通的,它会删除\n
我想Regex
用来确定什么是字母数字的所有内容,但它会在之后删除一些字母\n
回答by Rohit Jain
Try using this regex: -
尝试使用此正则表达式:-
str = str.replaceAll("(.+)(?<!\.)\n(?!\d)", " ");
System.out.println(str);
This will replace your \n
if it is not preceded
by a dot - termination of a paragraph
, and it is not followed
by a digit
, for when it is followed by a bulleted point. (like, your \n
in first bullet point is followed by a 1.2
. So, it will not be replaced.).
这将替换你的,\n
如果它是not preceded
由 a代替的dot - termination of a paragraph
,它是not followed
由 a digit
,因为它后面是一个项目符号。(例如,您\n
的第一个项目符号后跟一个1.2
。因此,它不会被替换。)。
(.+)
at the start, ensures that you are not replacing ablank line
.
(.+)
在开始时,确保您不会替换blank line
.
This will work for the string you have shown.
这将适用于您显示的字符串。
Explanation: -
解释: -
(.+)
-> A capture group, capturing anything, occurring at least once.(?<!\\.)
-> This is callednegative-look-behind
. It matches thestring
following it, only if that string is not preceded by adot(.)
given in thenegative-look-behind
pattern.
For e.g.: -You don't need to replace\n
after the line: -millions to billions of times more capable.\n
.(?!\\d)
-> This is callednegative -look-ahead
. It matches string behind it, only if that string is not followed by adigit (\\d)
given in thenegative-look-ahead
pattern.
For e.g.: -In your bulleted points,computers\n
is followed by1.2
. where1
is a digit. So, you don't want to replace that\n
.Now,
$1
and$2
represent the groups captured in the pattern match. Since you just want to replace"\n"
. So, we took the remaining pattern match as it is, while replacing"\n"
with aspace
.
(.+)
-> 一个捕获组,捕获任何东西,至少发生一次。(?<!\\.)
-> 这叫做negative-look-behind
. 它匹配string
后面的它,仅当该字符串之前没有在模式中dot(.)
给定negative-look-behind
。
例如: -您不需要\n
在行之后替换: -millions to billions of times more capable.\n
。(?!\\d)
-> 这叫做negative -look-ahead
. 它匹配后面的字符串,仅当该字符串后面没有模式中的digit (\\d)
给定negative-look-ahead
。
例如: -在您的项目符号中,computers\n
后跟1.2
. 哪里1
是数字。所以,你不想替换那个\n
。现在,
$1
并$2
表示在模式匹配捕获的组。由于您只想替换"\n"
. 因此,我们按原样采用剩余的模式匹配,同时替换"\n"
为space
.
So, $1
is representation for 1st group
- (.+)
所以,$1
是代表1st group
-(.+)
Note, look-ahead
and look-behind
regexes are non-capturing
groups.
注意,look-ahead
和look-behind
正则表达式的non-capturing
群体。
For More Details, follow these links: -
有关更多详细信息,请访问以下链接:-
回答by Vincent Jia
I suspect your requirement is to remove the \n of lines 1 and 2 .
What you can do is as below:
我怀疑您的要求是删除第 1 行和第 2 行的 \n 。
你可以做的是如下:
split your string into segments,
String[] array = yourString.split("\n");
concat every segments by adding \n tag, except line 1,2
array[1] + array[2] + array[3] + '\n' + array[4] + '\n' ...// and so forth
将你的字符串分成几段,
String[] array = yourString.split("\n");
通过添加 \n 标记连接每个段,第 1,2 行除外
数组[1] + 数组[2] + 数组[3] + '\n' + 数组[4] + '\n' ...// 等等