如何使用regex变量?

时间:2021-08-25 19:29:23

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

这是输入字符串:23 x ^ 45 * y或2 x ^ 2,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.

信后我匹配^[0 - 9]+ x。换句话说我匹配x ^数字紧随其后紧随其后。问题是,我不知道我是否匹配x,它可以是我在char数组中作为变量存储的任何字母。

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.

我是regex的新手,我也知道如果我定义regex变量,这是可以做到的,但是我不知道怎么做。

2 个解决方案

#1


3  

You can use the pattern @"[cEle]\^\d+" which you can create dynamically from your character array:

您可以使用模式@”肿物\ ^ \ d +”,你可以从你的角色创建动态数组:

string s = "23x^45*y or 2x^2 or y^4*x^3";
char[] letters = { 'e', 'x', 'L' };
string regex = string.Format(@"[{0}]\^\d+",
    Regex.Escape(new string(letters)));
foreach (Match match in Regex.Matches(s, regex))
    Console.WriteLine(match);

Result:

结果:

x^45
x^2
x^3

A few things to note:

有几点需要注意:

  • It is necessary to escape the ^ inside the regular expression otherwise it has a special meaning "start of line".
  • 有必要摆脱^在正则表达式否则它有特殊的意义“线”的开始。
  • It is a good idea to use Regex.Escape when inserting literal strings from a user into a regular expression, to avoid that any characters they type get misinterpreted as special characters.
  • 使用Regex是个好主意。当将用户的文字字符串插入到正则表达式中时,要进行转义,以避免输入的任何字符被误解为特殊字符。
  • This will also match the x from the end of variables with longer names like tax^2. This can be avoided by requiring a word boundary (\b).
  • 这也将结束与x变量名称较长的像税收^ 2。这可以通过使用单词边界(\b)来避免。
  • If you write x^1 as just x then this regular expression will not match it. This can be fixed by using (\^\d+)?.
  • 如果你把x ^ 1写成x这个正则表达式不匹配。这可以通过使用固定(\ ^ \ d +)?。

#2


0  

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

尝试使用此模式用于捕获数量但不包括x ^前缀:

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

(? < = 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.

如果需要,不要忘记使用选项IgnoreCase进行匹配。

#1


3  

You can use the pattern @"[cEle]\^\d+" which you can create dynamically from your character array:

您可以使用模式@”肿物\ ^ \ d +”,你可以从你的角色创建动态数组:

string s = "23x^45*y or 2x^2 or y^4*x^3";
char[] letters = { 'e', 'x', 'L' };
string regex = string.Format(@"[{0}]\^\d+",
    Regex.Escape(new string(letters)));
foreach (Match match in Regex.Matches(s, regex))
    Console.WriteLine(match);

Result:

结果:

x^45
x^2
x^3

A few things to note:

有几点需要注意:

  • It is necessary to escape the ^ inside the regular expression otherwise it has a special meaning "start of line".
  • 有必要摆脱^在正则表达式否则它有特殊的意义“线”的开始。
  • It is a good idea to use Regex.Escape when inserting literal strings from a user into a regular expression, to avoid that any characters they type get misinterpreted as special characters.
  • 使用Regex是个好主意。当将用户的文字字符串插入到正则表达式中时,要进行转义,以避免输入的任何字符被误解为特殊字符。
  • This will also match the x from the end of variables with longer names like tax^2. This can be avoided by requiring a word boundary (\b).
  • 这也将结束与x变量名称较长的像税收^ 2。这可以通过使用单词边界(\b)来避免。
  • If you write x^1 as just x then this regular expression will not match it. This can be fixed by using (\^\d+)?.
  • 如果你把x ^ 1写成x这个正则表达式不匹配。这可以通过使用固定(\ ^ \ d +)?。

#2


0  

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

尝试使用此模式用于捕获数量但不包括x ^前缀:

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

(? < = 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.

如果需要,不要忘记使用选项IgnoreCase进行匹配。