我有一個字符串s1(包含很多英語句子),一個字符串l1列表(其中包含像巴士,房子,火車,火車引擎,火車引擎的生活字數不會大於3)等)以及字符串l2(類似於l1)和整數n的列表。如何檢測字符串中的n個短語或單詞
對於l1的每個元素,我必須在s中找到l1或l2的其他元素(l1的元素可以在s中多次出現,我們必須在每次出現時找到它),以使它們的距離(距離是例如:我有xyz。在這種情況下,I和xyz之間的距離是2),從l1的元素的距離小於或等於l1元素兩邊的n。這些單詞/短語(l1或l2的元素)將被存儲在java中的列表中。
e.g.:
Input:
s= i am asd fgh and studying in zxc vbn bnmkl. i am 100 years old.(may contain other symbols too)
l1= i
l2=asd, fgh, zxc, vbn
n=3.
Output:
asd,fgh,zxc,vbn
for(int j=0;j<l1.size();j++){
String s1=l1.get(j);
int index = s.indexOf(s1);
while(index >= 0) {
System.out.println(index);
for(int k=0;k<n;){
// how to detect the words before and after that element of l1 such that distance is <= n on either side in a passage and they are contained in either l1 or l2.
}
index = s.indexOf(s1, index+1);
}
}
任何人都可以幫助我如何實現這個?
我們不會爲您生成代碼。開始編碼。回過頭來,當你有一些代碼並且詢問你不明白的部分的具體問題時。 – Turing85
你應該看看正則表達式。 –
我已添加代碼並在需要幫助的地方添加了該部分(作爲註釋)。 – Sam