[英]Regex pattern to get only particular part from List of String
由於我是 Regex 模式的新手,我想從下面提到的字符串列表中提取特定部分,並將其作為鍵值對存儲在 Map 中。
例子:
List<String> ref3Path = new ArrayList<String>();
ref3Path.add("s3://REF3/ca209_040/ahshd.csv");
ref3Path.add("s3://REF3/ca209_040/grren.csv");
ref3Path.add("s3://REF3/ca209_030/aestyyuae.csv");
我只想要上面列表中的ca209_040和aesae.csv並想存儲在 Map 中。
下面是我寫的比較列表和地圖的代碼:
public static void main(String[] args) {
// TODO Auto-generated method stub
MultiValuedMap<String, String> studyDomain = new ArrayListValuedHashMap<>();
List<String> ref3Path = new ArrayList<String>();
studyDomain.put("ca209_040", "czvv.csv");
studyDomain.put("ca209_040", "efe.csv");
studyDomain.put("ca209_030", "efef.csv");
studyDomain.put("ca209_030", "hhhjd.csv");
studyDomain.put("ca209_020", "rr.csv");
studyDomain.put("ca209_020", "eghh.csv");
ref3Path.add("s3://REF3/ca209_040/jlkjl.csv");
ref3Path.add("s3://REF3/ca209_040/aesaehkhk.csv");
ref3Path.add("s3://REF3/ca209_030/aesaedhd.csv");
ref3Path.add("null");
ref3Path.add("s3://REF3/ca209_020/aedae.csv");
ref3Path.add("s3://REF3/ca209_020/aeqwee.csv");
log.info("List of inbox: " +studyDomain);
log.info("List of ref3 :" +ref3Path);
rule1(studyDomain,ref3Path);
}
您可以使用簡單的split
功能來完成。 正則表達式模式是矯枉過正
注意:檢查邊界條件
scala> val arr = "s3://REF3/ca209_040/aesae.csv".split('/')
arr: Array[String] = Array(s3:, "", REF3, ca209_040, aesae.csv)
scala> arr(3)
res2: String = ca209_040
scala> arr(4)
res3: String = aesae.csv
注意:檢查邊界條件
public class Demo {
public static void main(String[] s) {
String[] args = "s3://REF3/ca209_040/aesae.csv".split("/");
System.out.println(args[3] + " , " + args[4]);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.