2010-04-28 21 views
0

MetaMap文件有如下行:正則表達式MetaMap在Java中

mappings([map(-1000,[ev(-1000,'C0018017','Objective','Goals',[objective],[inpr],[[[1,1],[1,1],0]],yes,no)])]). 

格式爲

mappings(
     [map(negated overall score for this mapping, 
      [ev(negated candidate score,'UMLS concept ID','UMLS concept','preferred name for concept - may or may not be different', 
       [matched word or words lowercased that this candidate matches in the phrase - comma separated list], 
       [semantic type(s) - comma separated list], 
       [match map list - see below],candidate involved with head of phrase - yes or no, 
       is this an overmatch - yes or no 
       ) 
      ] 
     ) 
     ] 
    ). 

我想運行在Java中的正則表達式查詢,讓我的琴絃「UMLS概念解釋ID「,語義類型和匹配地圖列表。 RegEx是正確的工具,還是在Java中完成此項工作的最有效方式?

回答

3

這是我嘗試使用正則表達式的解決方案。這個replace「元再生」方法是我正在試驗的東西;我想希望它讀取更可讀的代碼。

String line = "mappings([map(-1000,[ev(-1000,'C0018017','Objective','Goals',[objective],[inpr],[[[1,1],[1,1],0]],yes,no)])])."; 
String regex = 
    "mappings([map(number,[ev(number,<quoted>,quoted,quoted,[csv],[<csv>],[<matchmap>],yesno,yesno)])])." 
    .replaceAll("([\\.\\(\\)\\[\\]])", "\\\\$1") // escape metacharacters 
    .replace("<", "(").replace(">", ")") // set up capture groups 
    .replace("number", "-?\\d+") 
    .replace("quoted", "'[^']*'") 
    .replace("yesno", "(?:yes|no)") 
    .replace("csv", "[^\\]]*") 
    .replace("matchmap", ".*?") 
; 
System.out.println(regex); 
// prints "mappings\(\[map\(-?\d+,\[ev\(-?\d+,('[^']*'),'[^']*','[^']*',\[[^\]]*\],\[([^\]]*)\],\[(.*?)\],(?:yes|no),(?:yes|no)\)\]\)\]\)\." 

Matcher m = Pattern.compile(regex).matcher(line); 
if (m.find()) { 
    System.out.println(m.group(1)); // prints "'C0018017'" 
    System.out.println(m.group(2)); // prints "inpr" 
    System.out.println(m.group(3)); // prints "[[1,1],[1,1],0]" 
} 

replace元regexing讓您僅通過設置適當的replace(而不是灑所有到一個不可讀的混亂)輕鬆容納符號之間的空格。

+0

不錯的。順便說一句:10月份什麼夢想工作? – BalusC 2010-04-28 12:09:34

+1

我喜歡你的meta-regex方法!到目前爲止,我只使用了命名的字符串常量('String number =「 - ?\\ d +」')並將它們連接起來('... +「[ev(」+ number +「,」+ ...'),但那仍然導致醜陋的代碼。 – 2010-04-28 16:27:40

1

這是一個真正有毛的格式。正則表達式聽起來像是要走的路,但你將有一個真正的毛茸茸的正則表達式:

mappings\(\[map\(-?[0-9.]+,\[ev\(-?[0-9.]+,'(.*?)','.*?','.*?',\[.*?\],\[(.*?)\],\[(.*)\],(?:yes|no),(?:yes|no)\)\]\)\]\)\. 

當你要表達的正則表達式爲Java String更糟糕 - 一如既往,你會更換每\\\。但是這應該讓你得到你想要的;匹配的組1,2和3是您想要拔出的字符串。請注意,我沒有嚴格測試它是否對輸入格式不正確,因爲我沒有它的胃。 :)

教育目的:儘管它的外觀,這本來就不是很難在所有的構建 - 我只是把你的樣品線,取而代之的實際值與相應的通配符,確保逃離出來的括號和括號和結尾處的點。

1

這是可能的,是的。

類似於(假設您引用的值是唯一引號合法,您添加的值爲[]的唯一位置是那些合法的位置,即'['和']'字符。不能存在裏面的值,這門親事地圖列表不能]它除了在最後你得到的圖片 - 大量的假設)

^[^']+?'([^']*+)'[^\[]+\[[^]]+\],\[([^\]]*?)\],\[\[(.*?)\]\].*$ 

應該給。你那三個字段作爲三個匹配的組(在你的例子中使用http://www.regexplanet.com/simple/index.html進行測試)

這是 -

"^[^']+?'([^']*+)'[^\\[]+\\[[^]]+\\],\\[([^\\]]*?)\\],\\[\\[(.*?)\\]\\].*$" 

作爲Java字符串。 。 。

但這不是很好的維護。對於這個可能會更好一些!