How to use variables with regex?

后端 未结 2 879
孤街浪徒
孤街浪徒 2020-12-11 12:03

This is the input string: 23x^45*y or 2x^2 or y^4*x^3.

I am matching ^[0-9]+ after letter x. In other words I am matching

2条回答
  •  猫巷女王i
    2020-12-11 12:18

    Try using this pattern for capturing the number but excluding the x^ prefix:

    (?<=x\^)[0-9]+

    string strInput = "23x^45*y or 2x^2 or y^4*x^3";
    foreach (Match match in Regex.Matches(strInput, @"(?<=x\^)[0-9]+"))
        Console.WriteLine(match);
    

    This should print :

    45
    2
    3
    

    Do not forget to use the option IgnoreCase for matching, if required.

提交回复
热议问题