I have a string which has digit and words I want to use regex in java.
我有一个字符串,其中包含数字和单词,我想在java中使用正则表达式。
String s="S.No.DistrictMarketPrice 1 Agra Achhnera NIL 2 Agra Agra 1450 3 Agra Fatehabad NIL 4 Agra Fatehpur Sikri 8765 5 Agra Jagner NIL 6 Agra Jarar NIL 7 Agra Khairagarh NIL 8 Agra Shamshabad NIL 9 Aligarh Atrauli 1298 10 Aligarh Chharra NIL";
I want to break this string. I want every string in new line which should start from only exact serino
我想打破这个字符串。我希望新行中的每个字符串都应该从精确的serino开始
How can I get this output
我怎样才能得到这个输出
S.No .District Market Price
1 Agra Achhnera NIL
2 Agra Agra 1450
3 Agra Fatehabad NIL
4 Agra Fatehpur Sikri 8765
3 个解决方案
#1
8
You can replace each number by a new line + the number:
您可以用新行+数字替换每个数字:
String multiline = s.replaceAll("(\\d+)", "\n$1");
-
(\\d+)
captures the number -
$1
refers to the captured number
(\\ d +)捕获数字
$ 1表示捕获的数字
#2
2
Replace any instance of one or more digits with the system line separator followed by the matched digit(s).
将一个或多个数字的任何实例替换为系统行分隔符,后跟匹配的数字。
As such:
String s = "S.No.DistrictMarketPrice 1 Agra Achhnera NIL 2 Agra Agra NIL 3 Agra Fatehabad NIL 4 Agra Fatehpur Sikri NIL 5 Agra Jagner NIL 6 Agra Jarar NIL 7 Agra Khairagarh NIL 8 Agra Shamshabad NIL 9 Aligarh Atrauli NIL 10 Aligarh Chharra NIL";
System.out.println(
s.replaceAll("\\d+", System.getProperty("line.separator") + "$0")
);
Output
S.No.DistrictMarketPrice
1 Agra Achhnera NIL
2 Agra Agra NIL
3 Agra Fatehabad NIL
4 Agra Fatehpur Sikri NIL
5 Agra Jagner NIL
6 Agra Jarar NIL
7 Agra Khairagarh NIL
8 Agra Shamshabad NIL
9 Aligarh Atrauli NIL
10 Aligarh Chharra NIL
#3
1
Use this one:
使用这一个:
String []splits = s.split("(?<!\\d)(?=\\d+)");
splits contains lines per index:
splits包含每个索引的行数:
S.No.DistrictMarketPrice
1 Agra Achhnera NIL
2 Agra Agra NIL
3 Agra Fatehabad NIL
4 Agra Fatehpur Sikri NIL
5 Agra Jagner NIL
6 Agra Jarar NIL
7 Agra Khairagarh NIL
8 Agra Shamshabad NIL
9 Aligarh Atrauli NIL
10 Aligarh Chharra NIL
#1
8
You can replace each number by a new line + the number:
您可以用新行+数字替换每个数字:
String multiline = s.replaceAll("(\\d+)", "\n$1");
-
(\\d+)
captures the number -
$1
refers to the captured number
(\\ d +)捕获数字
$ 1表示捕获的数字
#2
2
Replace any instance of one or more digits with the system line separator followed by the matched digit(s).
将一个或多个数字的任何实例替换为系统行分隔符,后跟匹配的数字。
As such:
String s = "S.No.DistrictMarketPrice 1 Agra Achhnera NIL 2 Agra Agra NIL 3 Agra Fatehabad NIL 4 Agra Fatehpur Sikri NIL 5 Agra Jagner NIL 6 Agra Jarar NIL 7 Agra Khairagarh NIL 8 Agra Shamshabad NIL 9 Aligarh Atrauli NIL 10 Aligarh Chharra NIL";
System.out.println(
s.replaceAll("\\d+", System.getProperty("line.separator") + "$0")
);
Output
S.No.DistrictMarketPrice
1 Agra Achhnera NIL
2 Agra Agra NIL
3 Agra Fatehabad NIL
4 Agra Fatehpur Sikri NIL
5 Agra Jagner NIL
6 Agra Jarar NIL
7 Agra Khairagarh NIL
8 Agra Shamshabad NIL
9 Aligarh Atrauli NIL
10 Aligarh Chharra NIL
#3
1
Use this one:
使用这一个:
String []splits = s.split("(?<!\\d)(?=\\d+)");
splits contains lines per index:
splits包含每个索引的行数:
S.No.DistrictMarketPrice
1 Agra Achhnera NIL
2 Agra Agra NIL
3 Agra Fatehabad NIL
4 Agra Fatehpur Sikri NIL
5 Agra Jagner NIL
6 Agra Jarar NIL
7 Agra Khairagarh NIL
8 Agra Shamshabad NIL
9 Aligarh Atrauli NIL
10 Aligarh Chharra NIL