如何用正则表达式获得中间字符串?

时间:2018-03-12 04:11:38

标签: java regex

我有一个看起来像这样的输入字符串

DatalogSetupFile:BTS50xx1EJA \ 3.20 \ log_all.stp

filterFunction(input)<script type="text/javascript"> function filterFunction(table, input, total_amount_id) { var filter, tr, td, i, totalViewable = 0; console.log(input1.value); console.log(input); filter = input.value.toUpperCase(); tr = table.getElementsByTagName("tr"); for (i = 0; i < tr.length; i++) { td = tr[i].getElementsByTagName("td")[1]; tds = tr[i].getElementsByTagName("td")[0]; if (td) { if (td.innerHTML.toUpperCase().indexOf(filter) > -1) { tr[i].style.display = ""; totalViewable += parseFloat(tds.innerHTML); document.getElementById(total_amount_id).innerHTML = "$" + totalViewable.toFixed(2); } else { tr[i].style.display = "none"; } } } } var table1 = document.getElementById("dateTable"); var input1 = document.getElementById("event_date_range"); var total_amount_id1 = "total_amount_td"; filterFunction(table1, input1, total_amount_id1); var table2 = document.getElementById("dateTable2"); var input2 = document.getElementById("event_date_range2"); var total_amount_id2 = "total_amount_td2"; filterFunction(table2, input2, total_amount_id2); </script> 是不变的。我希望从字符串中提取DatalogSetupFile:。我该怎么办?

2 个答案:

答案 0 :(得分:1)

您可以创建一个正则表达式组,您可以在其中指定所有静态内容,然后指定整个组中的动态内容,以便您可以将动态内容作为整个组获取,

您可以将regex定义为以下

^(?: DatalogSetupFile:\ S)的(。*)(?:\ 3.20 \ log_all.stp)$

试试这个Demo

您可以在此处使用第一组获取动态字符串

答案 1 :(得分:0)

尝试使用此正则表达式:

\s\K[^\\]+

在Java中看起来像:

String myInputString = "DatalogSetupFile: BTS50xx1EJA\\3.20\\log_all.stp";
Pattern myPattern = Pattern.compile("\\s\\K[^\\\\]+");
Matcher myMatcher = Pattern.matcher(myInputString);
System.out.println(myMatcher.group(0));