如何用" /"分割字符串和" //"?

时间:2013-06-16 19:26:45

标签: java regex split wavefront

我正在尝试为Wavefront Object文件创建基本模型查看器,并且部分文件读取包括拆分每一行。面是基于斜杠定义的。这些是可以从维基百科解析的不同类型的面孔:

  f v1 v2 v3 v4 ... <- Face built of vertices
  f v1/vt1 v2/vt2 v3/vt3 ... <- Face built of vertices + textures
  f v1/vt1/vn1 v2/vt2/vn2 v3/vt3/vn3 ... <- Face built of vertices, textures, and normals
  f v1//vn1 v2//vn2 v3//vn3 ... <- Face built of vertices and normals

我一直在逐行读取每一行,如果以“f”开头,我会在删除整行的前两个字符后将其发送到另一个方法以创建面。

e.g。 f v1 v2 v3会转到v1 v2 v3

当我必须处理带有斜线的线条时,我认为我错误地将它们分开,因为我没有得到正确的结果。我目前正在使用这两个分割:

   string.split("/");
   string.split("//");

这是否会产生问题,因为split参数应该是正则表达式,而我正在尝试按正则表达式中常用的字符进行拆分?

1 个答案:

答案 0 :(得分:0)

Slash在正则表达式中并不特别。试试这个:

for (String facePart : input.replaceAll("^f "), "").split(" ")) {
    String[] data = facePart.split("/");
    String vertex = data[0];
    String texture = data.length > 1 ? data[1] : "";
    String normal = data.length > 2 ? data[2] : "";
    // do something with the info
    // variables will be blank if missing from input
}
相关问题