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

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

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

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

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

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

英文:

Try this, it should work the way you want:

.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

尝试以下代码:

String str = "./homework2 3,phone3,desc3,brand4\\n4,phone4,desc4,brand4\\n5,phone5,desc5,brand5";
String[] arrOfStr = str.split("[\\s\\n,]+");

for (String a : arrOfStr) 
    System.out.println(a); 

输出:

./homework2
3
phone3
desc3
brand4\n4
phone4
desc4
brand4\n5
phone5
desc5
brand5

编辑:

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

for (int i = 0 ; i < arrOfStr.length ; i++) { //YOUR LOOP !!!
    splitString(arrOfStr[i]); 
}
static void splitString(String str) 
{ 
    StringBuffer alpha = new StringBuffer(),  
    num = new StringBuffer(), special = new StringBuffer(); 

    for (int i=0; i<str.length(); i++) 
    { 
        if (Character.isDigit(str.charAt(i))) 
            num.append(str.charAt(i)); 
        else if(Character.isAlphabetic(str.charAt(i))) 
            alpha.append(str.charAt(i)); 
        else
            special.append(str.charAt(i)); 
    } 
    System.out.println(alpha); 
    System.out.println(num);    
} 

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

homework
2

3
phone
3
desc
3
brandn
44
phone
4
desc
4
brandn
45
phone
5
desc
5
brand
5

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

英文:

Try this code :

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

Out put :

./homework2
3
phone3
desc3
brand4\n4
phone4
desc4
brand4\n5
phone5
desc5
brand5

EDIT :

I found this function in the site : Link Here

		   for (int i = 0 ; i &lt; arrOfStr.length ; i++) { //YOUR LOOP !!!
			   splitString(arrOfStr[i]); 
		   }	   
}

static void splitString(String str) 
{ 
    StringBuffer alpha = new StringBuffer(),  
    num = new StringBuffer(), special = new StringBuffer(); 
      
    for (int i=0; i&lt;str.length(); i++) 
    { 
        if (Character.isDigit(str.charAt(i))) 
            num.append(str.charAt(i)); 
        else if(Character.isAlphabetic(str.charAt(i))) 
            alpha.append(str.charAt(i)); 
        else
            special.append(str.charAt(i)); 
    } 
    System.out.println(alpha); 
    System.out.println(num);    
} 
}

Combine the two functions and you get:

homework
2

3
phone
3
desc
3
brandn
44
phone
4
desc
4
brandn
45
phone
5
desc
5
brand
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+,您可以使用一个常见的量词。下面显示了相同的内容:

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

因为它导致了:

./homework2
3
phone3
desc3
brand4
4
phone4
desc4
brand4
5
phone5
desc5
brand5

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

英文:

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

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

As it leads to

./homework2
3
phone3
desc3
brand4
4
phone4
desc4
brand4
5
phone5
desc5
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:

确定