2015-07-20 68 views
-4

如何從段落中提取單詞「0U47NN4XMD8V」「已更新產品0U47NN4XMD8V的更新,可能需要長達1小時才能反映這些更改。請在一段時間後從產品搜索頁面檢查」使用java代碼?正則表達式的如何提取單詞「0U47NN4XMD8V」?

+2

捏,代碼衝刺。徹底混合並在腦中烘烤10-20年。 –

+0

你有沒有試過的代碼?什麼都可以顯示你正在使用什麼樣的系統? –

+0

句子和'單詞'每次都完全一樣嗎?如果是這樣,你可以使用'substring()'和'word'的索引。 –

回答

1
//This is your sentence you want to extract the code from 
String sentence = "Changes for update of product 0U47NN4XMD8V has been submitted,it may take up to 1 hour for these changes to be reflected"; 

//This is an array of strings, which is your sentence split by every space, 
//hence (" "), it can be (",") or anything else you wanna split by. 
//This array has a size of 22 since you can split your sentence into 22 parts for 
//every space and it starts at 0 since its Java 
String [] parts = sentence.split(" "); 

//We want the 6th element so we assign a string our 6th element 
//in our array - parts[5] since it is zero based 
String extracted = parts[5]; 

//do stuff 
+0

可能會增加一些長度檢查,如果段落小於6個單詞。 – wgitscht

+0

是的,我同意,除了我不知道他真的想要什麼,所以這是一個快速的「修復」 –

+0

我認爲這非常符合他的需求,因爲他添加了對他原來的問題的評論:「不會這個詞不會每次都會改變「和」每次我需要從消息中提取第六個字「 – wgitscht

0
String code = "0U47NN4XMD8V" // Or a get method where this comes from 

String message = "Changes for update of Product 0U47NN4XMD8V has been submitted,it may take up to 1 hour for these changes to be reflected"; 

if(message.contains(code)){ 
    //do something 
}