英文:
How to use Pattern, Matcher in Java regex API to remove a specific line
问题
["-122","+","2","?","s","a","b"]
英文:
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)
答案1
得分: 2
- 使用正则表达式
\\s*\\?\\s*\\w+\\s*(?=\\?)
将不需要的字符串替换为""
。您可以链接String#replaceAll
来删除任何剩余的空白字符。注意,?=
表示正向预查,意思是\s*\?\s*\w+\s*
后跟一个?
。我希望您已经知道\s
表示空白字符,\w
表示单词字符。 - 然后您可以使用正则表达式
^-\d+|\d+|\D
,它表示开头的负整数(即^-\d+
),或者数字(即\d+
),或者非数字字符(\D
)。
示例代码:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String str = "-122+2 ? comments ?sa b";
str = str.replaceAll("\\s*\\?\\s*\\w+\\s*(?=\\?)", "").replaceAll("\\s+", "");
Pattern pattern = Pattern.compile("^-\\d+|\\d+|\\D");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}
输出结果:
-122
+
2
?
s
a
b
英文:
- Replace the unwanted string
\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. - Then you can use the regex,
^-\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:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String str = "-122+2 ? comments ?sa b";
str = str.replaceAll("\\s*\\?\\s*\\w+\\s*(?=\\?)", "").replaceAll("\\s+", "");
Pattern pattern = Pattern.compile("^-\\d+|\\d+|\\D");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}
Output:
-122
+
2
?
s
a
b
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论