2012-06-12 51 views
0

鑑於輸入字符串可以如下規定:正則表達式和字符串操作技術

  • 讀或
  • 的xpath( '... ')或
  • (的xpath(' ...'))
  • ...

凡...只是擁有一些XPath表達式,例如,/comment/text

我真正想要的是XPath表達式;考慮到可能指定的三種可能的有效模式,在一般情況下提取此值將是一種有效的方法。

此外,我正在Java中實現這一點。

+1

你不需要爲正則表達式startsWith()和substring()就足夠了。 –

回答

0

下面是簡單的例子,它在您的兩個字符串舉例XPath部分相匹配。

import java.util.regex.*; 

class Untitled { 
    public static void main(String[] args) { 
    String input = "read(xpath('...'))"; 
    String result = null; 
    Pattern regex = Pattern.compile("xpath\\(\'(.*?)\'\\)"); 
    Matcher matcher = regex.matcher(input); 

    if (matcher.find()) { 
     result = matcher.group(1); 
    } 

    System.out.println(result); 
    } 
} 
+0

謝謝,我喜歡解決方案... – Larry