java 固定格式字符串中截取两个数字和子字符串
如题,有多行固定格式的字符串,如下Siegen->Osnabrueck[label="22,100"];FrankfurtanderOder->Kassel[label=...
如题,有多行固定格式的字符串,如下
Siegen -> Osnabrueck [label="22,100"];
FrankfurtanderOder -> Kassel [label="68,80"];
FrankfurtanderOder -> Potsdam [label="89,120"];
Goeteborg -> Aachen [label="30,200"];
Dresden -> Duisburg [label="84,120"];
Bremen -> Duisburg [label="8,80"];
Bremen -> Chemnitz [label="52,80"];
Bremen -> Bochum [label="49,50"];
Duesseldorf -> Dresden [label="76,130"];
Duesseldorf -> Mannheim [label="60,50"];
Duesseldorf -> Bochum [label="45,50"];
Aachen -> Giessen [label="41,100"];
Mainz -> Siegen [label="85,80"];
Mainz -> Luebeck [label="80,100"];
如何截取->两边的城市名,以及=后面的两个数字,城市名返回String型,数字返回double型? 展开
Siegen -> Osnabrueck [label="22,100"];
FrankfurtanderOder -> Kassel [label="68,80"];
FrankfurtanderOder -> Potsdam [label="89,120"];
Goeteborg -> Aachen [label="30,200"];
Dresden -> Duisburg [label="84,120"];
Bremen -> Duisburg [label="8,80"];
Bremen -> Chemnitz [label="52,80"];
Bremen -> Bochum [label="49,50"];
Duesseldorf -> Dresden [label="76,130"];
Duesseldorf -> Mannheim [label="60,50"];
Duesseldorf -> Bochum [label="45,50"];
Aachen -> Giessen [label="41,100"];
Mainz -> Siegen [label="85,80"];
Mainz -> Luebeck [label="80,100"];
如何截取->两边的城市名,以及=后面的两个数字,城市名返回String型,数字返回double型? 展开
1个回答
展开全部
用正则表达式吧,我把匹配出的字符串和数字都放到了动态数组ArrayList中以方便你取用.
完整的程序如下:
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class KKK {
public static void main(String[] args) {
//定义String和Double泛型的ArrayList动态数组来分别存放出发城市,到达城市以及等号后面的两个数字
ArrayList<String> chufa=new ArrayList<String>();
ArrayList<String> daoda=new ArrayList<String>();
ArrayList<Double> x=new ArrayList<Double>();
ArrayList<Double> y=new ArrayList<Double>();
//用正则表达式匹配每一行信息
String s="Siegen -> Osnabrueck [label=\"22,100\"];\nFrankfurtanderOder -> Kassel [label=\"68,80\"];\nFrankfurtanderOder -> Potsdam [label=\"89,120\"];\nGoeteborg -> Aachen [label=\"30,200\"];\nDresden -> Duisburg [label=\"84,120\"];\nBremen -> Duisburg [label=\"8,80\"];\nBremen -> Chemnitz [label=\"52,80\"];\nBremen -> Bochum [label=\"49,50\"];\nDuesseldorf -> Dresden [label=\"76,130\"];\nDuesseldorf -> Mannheim [label=\"60,50\"];\nDuesseldorf -> Bochum [label=\"45,50\"];\nAachen -> Giessen [label=\"41,100\"];\nMainz -> Siegen [label=\"85,80\"];\nMainz -> Luebeck [label=\"80,100\"];";
String regex="([\\w\\s]+)->([\\w\\s]+)\\[label=\"([\\d\\.]+)\\,([\\d\\.]+)\"\\];";
Pattern p=Pattern.compile(regex);
Matcher m=p.matcher(s);
while(m.find()){
chufa.add(m.group(1).trim());
daoda.add(m.group(2).trim());
x.add(Double.parseDouble(m.group(3)));
y.add(Double.parseDouble(m.group(4)));
}
//输出ArrayList中的值
for(int i=0;i<chufa.size();i++){
System.out.println((String)chufa.get(i));
System.out.println((String)daoda.get(i));
System.out.println((Double)x.get(i));
System.out.println((Double)y.get(i));
}
}
}
运行结果:
Siegen
Osnabrueck
22.0
100.0
FrankfurtanderOder
Kassel
68.0
80.0
FrankfurtanderOder
Potsdam
89.0
120.0
Goeteborg
Aachen
30.0
200.0
Dresden
Duisburg
84.0
120.0
Bremen
Duisburg
8.0
80.0
Bremen
Chemnitz
52.0
80.0
Bremen
Bochum
49.0
50.0
Duesseldorf
Dresden
76.0
130.0
Duesseldorf
Mannheim
60.0
50.0
Duesseldorf
Bochum
45.0
50.0
Aachen
Giessen
41.0
100.0
Mainz
Siegen
85.0
80.0
Mainz
Luebeck
80.0
100.0
更多追问追答
追问
谢谢啊!若字符串还有
A [label="A,0"];
B [label="B,2"];
C [label="C,3"];
正则表达式又该如何写?另外,若这些字符串以;分割,按行读入,是不是就不用加那个While循环?实际上这些字符串存在一个ArrayList s变量里,是不是只要控制下标0到s.size()-1就能得到所有分量?
追答
你是说A [label="A,0"];这种形式的数据还是C->A[label="A,0"]这种有出发地的数据形式.
另外,象A [label="A,0"];这种形式的数据是混在上面的C->A[label="A,0"]这种形式的数据中,还是存储在另外的动态数组ArrayList中.
如果这些字符串存在动态数组ArrayList s变量里,就象你说的只要用for循环一个一个取出来,不用while循环,只要用if(m.matches())匹配一下就行了.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询