2015-04-04 27 views
0

我有這個輸入:正則表達式匹配的煩惱

;Client = tefexx;Test = tgrfdrff;Piemel = thgfress 

而這正則表達式:

(;Client =) 

在正則表達式的字將取決於需求的變化。但在這種情況下,我只想返回tefexx。我不明白如何匹配這個詞。

回答

2

你可以試試這個:

(;Client = (.*?);) 

在你爲例,這個正則表達式的第二個捕獲組將舉行「tefexx」而已。

0

這應該工作/Client = ([a-zA-z]+);/

0

下面是使用PatternMatcher一個例子:

import java.util.regex.Pattern; 
import java.util.regex.Matcher; 

public class Re { 
    static String s = ";Client = tefexx;Test = tgrfdrff;Piemel = thgfress"; 
    static String re = ";Client = ([^;]*);"; 

    static public void main(String[] args) { 
     Pattern pattern = Pattern.compile(re); 
     Matcher matcher = pattern.matcher(s); 
     if (matcher.find()) { 
      System.out.println(matcher.group(1)); 
     } 
    } 
} 


$ java Re 
tefexx