我在用Ruby數年的間隙後返回Java。我正在尋找的是完成以下Ruby代碼習慣和短Java代碼:將簡單的Ruby正則表達式轉換爲Java
some_string.scan(/[\w|\']+/)
上述表達式創建一個字符串數組。陣列中的元素是由任一alphanum字符(\w
)或撇號的的some_string
所有部分
例如(\'
使「約翰的」不分成兩個單詞。):
"(The farmer's daughter) went to the market".scan(/[\w|\']+/)
=>
["The", "farmer's", "daughter", ...]
我知道該解決方案將使用這樣的事情:
String[] words = sentence.split(" ");
我只需要一個進去split()
正則表達式的一部分。
我知道java和java中的正則表達式 - 但我不明白你在做什麼Ruby正則表達式。你能用言語說出來嗎? :) – Mads 2012-04-18 23:07:45
我澄清了上面的正則表達式。 – bevanb 2012-04-18 23:15:42
你不需要'|'在一個字符類中(括號括起來[]'),並且你不需要轉義'''。正則表達式'/ [\ w'] + /'是正確的,而你的是錯誤的。 – 2012-04-19 01:02:25