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 x followed by ^ followed by numbers. Problem is that I don’t know that I am matching x, it could be any letter that I stored as variable in my char array.
For example:
foreach (char cEle in myarray) // cEle is letter in char array x, y, z, ...
{
match CEle in regex(input) //PSEUDOCODE
}
I am new to regex and I new that this can be done if I define regex variables, but I don’t know how.
You can use the pattern
@"[cEle]\^\d+"which you can create dynamically from your character array:Result:
A few things to note:
^inside the regular expression otherwise it has a special meaning “start of line”.Regex.Escapewhen inserting literal strings from a user into a regular expression, to avoid that any characters they type get misinterpreted as special characters.tax^2. This can be avoided by requiring a word boundary (\b).x^1as justxthen this regular expression will not match it. This can be fixed by using(\^\d+)?.