2013-09-24 41 views
0

我尝试在文本文件中查找特定字符串。 该文件看起来像这样:在文本文件中查找字符串

2,1,'Ausbau der techn。原基”

2,2, '拓德装置技术'

2,3, 'Estensione阿尔istallazioni tecniche'

我试图找到之间的文本'的迹象。

//Will be set automaticly after implementation. 
    int project = 2 
    int languageInteger = 1 
    String findings = new File(usedPath)?.eachLine { 

      it.substring((project+ ", " + languageInteger + ", ")) 

    } 

这是行不通的。我也试过用FindAll Closure或find。但我犯了一些错误。

我应该怎么做才能找到文本?

+0

这篇文章解释[如何获得引号之间的字符串数据(http://stackoverflow.com/questions/1473155/how-to-get-data-between-quotes-in-java)。你必须用正则表达式替换正则表达式中的'''' – Kaadzia

+0

不,我知道“ist not' 我在下面的解决方案中做了一个replaceAll(”'“,”“),它工作正常现在。 – CollinG

+0

我的意思是如果你打算使用教程中的正则表达式:'Pattern.compile(“\”([^ \“] *)\”“);''你必须替换'''' - ) – Kaadzia

回答

0

我找到了解决方案。 它不会是最好的,但它的工作原理。

new File(usedPath)?.eachLine { 

     if(it?.toString()?.startsWith(project+ ", " + languageInteger + ", ")){ 
      findings = it?.toString()?.split(project+ ", " + languageInteger + ", ")?.getAt(1) 
     } 
    } 
+0

使用'java.util.regex.Matcher'会更漂亮。 [这是一个教程](http://tutorials.jenkov.com/java-regex/matcher.html),如果你想改变你的代码。 – Kaadzia