Java Regex,提取第二个语句

时间:2017-06-21 15:34:11

标签: java regex

我有这种情况,我想提取该字符串表达式的statement1,但我不是正则表达式的专家。

Z

但是statement1可以递归地表达另一个表达式,如:

if(X = 3, Z, Y)

在这种情况下,我需要提取String IF_CONDITION_PATTERN = "[\\s|\\n|\\t]if\\s*\\((.*)\\,(.*)\\,(.*)([^)]*?)"; String formulaStr = "blablaba iF(X = 6, if(X = 3, Z, Y), Y) bablabla"; Pattern pattern = Pattern.compile(IF_CONDITION_PATTERN, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(formulaStr); while (matcher.find()) { for(int i=0; i<matcher.groupCount();i++){ System.out.println("Found " + i + ": " + matcher.group(i) + "."); } } ,或至少Found 0: iF(X = 6, if(X = 3, Z, Y), Y) bablabla. Found 1: X = 6, if(X = 3, Z. Found 2: Y). Found 3: Y) bablabla.

我做到了:

if(X = 3, Z, Y)

结果是:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
     string item = dataGridView1.SelectedCells[0].Value.ToString();
     comboBox1.SelectedItem = item;
}

有人可以帮助我至少提取声明1 <div class="hidden-xs margin-top-xs padding-top-xs">...</div>

谢谢, 努诺

1 个答案:

答案 0 :(得分:0)

如果下面的正则表达式适用于您,请为您的示例匹配statement1Z

正则表达式只是查找if语句然后匹配第二个参数,其中不包含,

\bif\([^,]+,\s?([^,]+),[^,]+\)

demo