英文:
Use String.split() remove a custom defined sub-string
问题
我需要根据自定义定义的子字符串“$ This is a Comments $”和“$ ThisISalsoAComments $”来拆分字符串。
因此,字符串
“Question 1 $ MultipleChoice $ Question 2 $ One Choice $ Question 3 $ $$$$ $”
将在拆分后得到以下字符串
["Question", "1", "Question", "2", "Question", "3"]
“$<Space>anyStringhere<Space>$”是一个注释,这实际上是通过拆分从字符串中删除所有注释。
英文:
I need to split string based on a custom defined sub-string $ This is a Comments $
$ ThisISalsoAComments $
So the string
Question 1 $ MultipleChoice $ Question 2 $ One Choice $ Question 3 $ $$$$ $
Will get the following string after split
["Question", "1", "Question", "2", "Question", "3"]
$<Space>anyStringhere<Space>$
is a comment, this is in fat removing all the comments from the string by split
答案1
得分: 3
以下是翻译好的内容:
String regex = " (?:\$ .*? \$(?: |$))?";
String input = "问题 1 $ 多项选择 $ 问题 2 $ 单项选择 $ 问题 3 $ $$$$ $";
String[] result = input.split(regex);
System.out.println(Arrays.toString(result));
输出
[问题, 1, 问题, 2, 问题, 3]
根据您的需求细节,您可能希望将正则表达式中的空格替换为 \\s
或 \\s+
。
解释
" " 匹配空格(单词分隔符或注释开头)
"(?: 开始可选的非捕获组:
"\$ " 匹配 '$ '(注释开始标记)
".*?" 懒惰匹配任意内容,即匹配注释内容
" \$" 匹配 ' $'(注释结束标记)
"(?: |$)" 匹配在尾部的 '$' 后的空格,或者匹配输入的结尾
")?" 结束可选的非捕获组
英文:
The following regex will do it:
String regex = " (?:\$ .*? \$(?: |$))?";
String input = "Question 1 $ MultipleChoice $ Question 2 $ One Choice $ Question 3 $ $$$$ $";
String[] result = input.split(regex);
System.out.println(Arrays.toString(result));
Output
[Question, 1, Question, 2, Question, 3]
Depending on the details of your requirements, you might want to replace the spaces in the regex with \\s
or \\s+
.
Explanation
" " Match a space (word separator, or beginning of comment)
"(?:" Start of optional non-capturing group:
"\$ " Match '$ ' (start-of-comment marker)
".*?" Match anything reluctantly, i.e. match the comment content
" \$" Match ' $' (end-of-comment marker)
"(?: |$)" Match a space after the trailing '$', or match end of input
")?" End of optional non-capturing group
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论