简体   繁体   English

正则表达式:从格式化的值中提取子字符串

[英]Regular expressions: extract a substring from a formatted value

I need to extract a substring from a formatted value as follows: 我需要从格式化的值中提取子字符串,如下所示:

“(The original reference for 'item1' is: 12345)” “('item1'的原始参考是:12345)”

The text that I need is 12345. 'item1' can change, although the rest of the string should remain static. 我需要的文本是12345.“item1”可以更改,但字符串的其余部分应保持静态。

I currently have something like this: 我目前有这样的事情:

  string myString = “(The original reference for ‘item1’ is: 12345)”;
  string regexMatch = "(The original reference for .* is: ";
  Regex regex = new Regex(regexMatch);
  Console.WriteLine(regex.Match(myString).ToString());

This just errors saying I need a closing bracket. 这只是错误说我需要一个结束括号。 Can someone point me in the right direction on this one, please? 有人能指出我在这个方向上正确的方向吗?

You need to escape the ( . 你需要逃避(

string regexMatch = @"\(The original reference for .* is: ";

Note that @ sign, which causes the compiler to not process escape sequences in the string. 请注意@符号,这会导致编译器不处理字符串中的转义序列。
Otherwise, you would need to escape the \\ itself from the compiler, like this: "\\\\(..." . 否则,您需要从编译器中转义\\本身,如下所示: "\\\\(..."

Also, you probably want a lazy wildcard: 此外,您可能想要一个懒惰的通配符:

string regexMatch = @"\(The original reference for .*? is: ";

You want the number in this textual context, right? 你想要这个文本背景中的数字,对吗? So try this regex: 所以试试这个正则表达式:

string regexMatch = @"(?<=\(The original reference for '[^']+' is: *)\d+(?=\))";

The value of the match will then be the number (nothing else). 然后匹配的值将是数字(没有别的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM