使用String.split()删除自定义定义的子字符串

huangapple go评论113阅读模式
英文:

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

[&quot;Question&quot;, &quot;1&quot;, &quot;Question&quot;, &quot;2&quot;, &quot;Question&quot;, &quot;3&quot;]

$&lt;Space&gt;anyStringhere&lt;Space&gt;$ 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 = &quot; (?:\$ .*? \$(?: |$))?&quot;;

String input = &quot;Question 1 $ MultipleChoice $ Question 2 $ One Choice $ Question 3 $ $$$$ $&quot;;
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

&quot; &quot;         Match a space (word separator, or beginning of comment)
&quot;(?:&quot;       Start of optional non-capturing group:
  &quot;\$ &quot;      Match &#39;$ &#39; (start-of-comment marker)
  &quot;.*?&quot;       Match anything reluctantly, i.e. match the comment content
  &quot; \$&quot;      Match &#39; $&#39; (end-of-comment marker)
  &quot;(?: |$)&quot;   Match a space after the trailing &#39;$&#39;, or match end of input
&quot;)?&quot;        End of optional non-capturing group

huangapple
  • 本文由 发表于 2020年10月18日 07:53:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64408435.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定