使用换行正则表达式后跟一个数字来拆分字符串 (JAVA)

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

Splitting a string using a next line regex followed by a digit (JAVA)

问题

我有以下需要根据空格、逗号和换行符进行分割的字符串。
示例字符串:“./homework2 3,phone3,desc3,brand4\n4,phone4,desc4,brand4\n5,phone5,desc5,brand5”。

我正在使用以下分割函数:.split(""[,\\s+\\n+]");,但是我无法获得所需的输出。程序继续将brand4\n4读作一个子字符串。
希望能得到帮助。

英文:

I have the following string which I need to split on space, commas, and next line character.
Sample string: "./homework2 3,phone3,desc3,brand4\n4,phone4,desc4,brand4\n5,phone5,desc5,brand5"

I am using split function as: .split("[,\\s+\\n+]"); but I am unable to get the required out put. The program keeps reading brand4\n4 as one substring.

Help would be appreciated.

答案1

得分: 0

尝试这样做,它应该按你想要的方式工作:

  1. .split(" |,|\\n");

这将在空格 (" "), 逗号或换行符处分割字符串。如果需要,使用或运算符添加更多条目。

英文:

Try this, it should work the way you want:

  1. .split(" |,|\\n");

This splits the string at a space (" "), a comma, or a next line character. Use the or operator to add more entries if needed.

答案2

得分: 0

尝试以下代码:

  1. String str = "./homework2 3,phone3,desc3,brand4\\n4,phone4,desc4,brand4\\n5,phone5,desc5,brand5";
  2. String[] arrOfStr = str.split("[\\s\\n,]+");
  3. for (String a : arrOfStr)
  4. System.out.println(a);

输出:

  1. ./homework2
  2. 3
  3. phone3
  4. desc3
  5. brand4\n4
  6. phone4
  7. desc4
  8. brand4\n5
  9. phone5
  10. desc5
  11. brand5

编辑:

我在这个网站上找到了这个函数:链接在这里

  1. for (int i = 0 ; i < arrOfStr.length ; i++) { //YOUR LOOP !!!
  2. splitString(arrOfStr[i]);
  3. }
  1. static void splitString(String str)
  2. {
  3. StringBuffer alpha = new StringBuffer(),
  4. num = new StringBuffer(), special = new StringBuffer();
  5. for (int i=0; i<str.length(); i++)
  6. {
  7. if (Character.isDigit(str.charAt(i)))
  8. num.append(str.charAt(i));
  9. else if(Character.isAlphabetic(str.charAt(i)))
  10. alpha.append(str.charAt(i));
  11. else
  12. special.append(str.charAt(i));
  13. }
  14. System.out.println(alpha);
  15. System.out.println(num);
  16. }

将这两个函数组合起来,你会得到:

  1. homework
  2. 2
  3. 3
  4. phone
  5. 3
  6. desc
  7. 3
  8. brandn
  9. 44
  10. phone
  11. 4
  12. desc
  13. 4
  14. brandn
  15. 45
  16. phone
  17. 5
  18. desc
  19. 5
  20. brand
  21. 5

你可以将这些值存储在另一个数组中,或者选择其他方式。

英文:

Try this code :

  1. String str = &quot;./homework2 3,phone3,desc3,brand4\\n4,phone4,desc4,brand4\\n5,phone5,desc5,brand5&quot;;
  2. String[] arrOfStr = str.split(&quot;[\\s\\n,]+&quot;);
  3. for (String a : arrOfStr)
  4. System.out.println(a);
  5. }
  6. }

Out put :

  1. ./homework2
  2. 3
  3. phone3
  4. desc3
  5. brand4\n4
  6. phone4
  7. desc4
  8. brand4\n5
  9. phone5
  10. desc5
  11. brand5

EDIT :

I found this function in the site : Link Here

  1. for (int i = 0 ; i &lt; arrOfStr.length ; i++) { //YOUR LOOP !!!
  2. splitString(arrOfStr[i]);
  3. }
  4. }
  5. static void splitString(String str)
  6. {
  7. StringBuffer alpha = new StringBuffer(),
  8. num = new StringBuffer(), special = new StringBuffer();
  9. for (int i=0; i&lt;str.length(); i++)
  10. {
  11. if (Character.isDigit(str.charAt(i)))
  12. num.append(str.charAt(i));
  13. else if(Character.isAlphabetic(str.charAt(i)))
  14. alpha.append(str.charAt(i));
  15. else
  16. special.append(str.charAt(i));
  17. }
  18. System.out.println(alpha);
  19. System.out.println(num);
  20. }
  21. }

Combine the two functions and you get:

  1. homework
  2. 2
  3. 3
  4. phone
  5. 3
  6. desc
  7. 3
  8. brandn
  9. 44
  10. phone
  11. 4
  12. desc
  13. 4
  14. brandn
  15. 45
  16. phone
  17. 5
  18. desc
  19. 5
  20. brand
  21. 5

You can store the values in another array, or whatever you choose.

答案3

得分: 0

请查看以下链接以验证正则表达式:

https://regex101.com/r/2ZkRBp/1/

英文:

Check the below link to validate regex

https://regex101.com/r/2ZkRBp/1/

答案4

得分: 0

代替使用 \s+ 和 \n+,您可以使用一个常见的量词。下面显示了相同的内容:

  1. String s = "./homework2 3,phone3,desc3,brand4\n4,phone4,desc4,brand4\n5,phone5,desc5,brand5";
  2. String[] s1 = s.split("[,\\s\\n]+");
  3. for (String s2 : s1) {
  4. System.out.println(s2);
  5. }

因为它导致了:

  1. ./homework2
  2. 3
  3. phone3
  4. desc3
  5. brand4
  6. 4
  7. phone4
  8. desc4
  9. brand4
  10. 5
  11. phone5
  12. desc5
  13. brand5

如果您有任何其他问题,欢迎提出。

英文:

Instead of using \s+ and \n+ you could use a common quantifier. The below depicts the same

  1. String s = &quot;./homework2 3,phone3,desc3,brand4\n4,phone4,desc4,brand4\n5,phone5,desc5,brand5&quot;;
  2. String[]s1 = s.split(&quot;[,\\s\\n]+&quot;);
  3. for(String s2:s1) {
  4. System.out.println(s2);
  5. }

As it leads to

  1. ./homework2
  2. 3
  3. phone3
  4. desc3
  5. brand4
  6. 4
  7. phone4
  8. desc4
  9. brand4
  10. 5
  11. phone5
  12. desc5
  13. brand5

huangapple
  • 本文由 发表于 2020年5月30日 00:11:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/62090246.html
匿名

发表评论

匿名网友

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

确定