2011-05-25 40 views
2

我做了一些文件名以VBS刮,如果文件名被稱爲VBS正則表達式:匹配只有第2之間的文本強調

hello_world_2012_is_not the end of the world.pdf 

那麼正則表達式應該匹配單詞「世界」 沒有別的

我嘗試這樣做:

[_][^_]+(?=_) 

,但它得到的一切,是兩個下劃線之間。我如何只選擇第一次出現?

+0

你想知道文件名是否包含單詞「world」? – freitass 2011-05-25 17:44:56

+0

對不起......在標題 – freitass 2011-05-25 17:46:08

+0

處看到了答案好點。不,我想提取它,所以它可能是任何事情......恐怕不像尋找「世界」那麼簡單。 – EKet 2011-05-25 17:46:18

回答

4

我建議以下的正則表達式:

/^[^_]*_([^_]*)_/ 

說明:

^  # Anchor the search to the start of the string 
[^_]* # Match any number of non-underscores, don't capture them 
_  # Match the first underscore 
([^_]*) # Match any number of non-underscores, do capture them 
_  # Match the second underscore. 

然後第一個捕獲組($1)將包含world和字符串中的正則表達式不會匹配其他地方。

+0

優秀!不需要使用字符串操縱器來提取下劃線。非常感謝你 – EKet 2011-05-25 19:50:26

4

正則表達式本身就應該是這個樣子:

_([^_]*)_ 

字符串被捕獲到組1

另外,使用字符串函數,找到第一個2個下劃線,然後提取它們之間有什麼。