正则表达式在括号与星号配对之间提取文本

时间:2022-09-13 00:05:10

This is a slightly different from similar posts in that the parenthesis are paired with an asterisk. example input:

这与类似的帖子略有不同,因为括号与星号配对。示例输入:

yada yada (* need to grab this text *) yoda

yada yada(*需要抓住这个文字*)yoda

I thought Jennifers post could be altered for this but my attempts fail.

我认为Jennifers的帖子可以改变,但我的尝试失败了。

       //Regex regex = new Regex("\\((?<TextInsideBrackets>\\w+)\\)"); //original
        Regex regex = new Regex("\\(\\*(?<TextInsideBrackets>\\w+)\\*\\)"); // my attempt
        string incomingValue = "Autocycleprestartcase := 20;   (* Yayitme ve Konveyoru bosaltabilir *)";
        string insideBrackets = null;
        Match match = regex.Match(incomingValue);
        if (match.Success)
        {
            insideBrackets = match.Groups["TextInsideBrackets"].Value;
        }

Suggestions?

Also, I'd like to remove the enclosed text, with the enclosing parethesis/asterisk pairs, from the input line.

另外,我想从输入行中删除带有封闭的parethesis /星号对的封闭文本。

So the output of above would give me

所以上面的输出会给我

yada yada yoda

yada yada yoda

and the value

和价值

need to grab this text

需要抓住这个文本

Thanks

2 个解决方案

#1


1  

Change it to

将其更改为

Regex regex = new Regex("\\(\\*(?<TextInsideBrackets>[\\w ]+)\\*\\)"); 
                                                     ^^^^^^

to allow spaces

允许空间

#2


1  

Here is a solution to get both the values while re-using the pattern dynamically:

这是一个在动态重用模式的同时获取两个值的解决方案:

string incomingValue = "Autocycleprestartcase := 20;   (* Yayitme ve Konveyoru bosaltabilir *)";
string pattern = @"\(\*\s*(.*?)\s*\*\)";
string insideBrackets = Regex.Match(incomingValue, pattern).Groups[1].Value ?? string.Empty;
Console.WriteLine(insideBrackets); // => Yayitme ve Konveyoru bosaltabilir 
Console.WriteLine(Regex.Replace(incomingValue, $@"\s*{pattern}", string.Empty)); // => Autocycleprestartcase := 20;

See the C# demo

请参阅C#演示

Pattern details:

  • \( - a literal ( (note the single backslash is used as the string is defined via a verbatim string literal, @"")
  • \( - 一个文字((注意单个反斜杠用作字符串是通过逐字字符串文字来定义的,@“”)

  • \* - a literal *
  • \ * - 文字*

  • \s* - 0+ whitespaces (trimming the value from the left)
  • \ s * - 0+空格(修剪左边的值)

  • (.*?) - Group 1 capturing zero or more chars other than newline, as few as possible, up to the first occurrence of the subsequent subpatterns
  • (。*?) - 第1组捕获除换行之外的零个或多个字符,尽可能少,直到第一次出现的后续子模式

  • \s* - zero or more whitespaces (trimming from the right)
  • \ s * - 零个或多个空格(从右边修剪)

  • \* - a literal *
  • \ * - 文字*

  • \) - a literal )
  • \) - 文字)

To get the second value, you may use the same pattern, but add \s* (zero or more whitespaces) at the beginning, what is done with Regex.Replace(incomingValue, $@"\s*{pattern}", string.Empty).

要获得第二个值,您可以使用相同的模式,但在开头添加\ s *(零个或多个空格),使用Regex.Replace(incomingValue,$ @“\ s * {pattern}”,字符串完成的操作.Empty)。

#1


1  

Change it to

将其更改为

Regex regex = new Regex("\\(\\*(?<TextInsideBrackets>[\\w ]+)\\*\\)"); 
                                                     ^^^^^^

to allow spaces

允许空间

#2


1  

Here is a solution to get both the values while re-using the pattern dynamically:

这是一个在动态重用模式的同时获取两个值的解决方案:

string incomingValue = "Autocycleprestartcase := 20;   (* Yayitme ve Konveyoru bosaltabilir *)";
string pattern = @"\(\*\s*(.*?)\s*\*\)";
string insideBrackets = Regex.Match(incomingValue, pattern).Groups[1].Value ?? string.Empty;
Console.WriteLine(insideBrackets); // => Yayitme ve Konveyoru bosaltabilir 
Console.WriteLine(Regex.Replace(incomingValue, $@"\s*{pattern}", string.Empty)); // => Autocycleprestartcase := 20;

See the C# demo

请参阅C#演示

Pattern details:

  • \( - a literal ( (note the single backslash is used as the string is defined via a verbatim string literal, @"")
  • \( - 一个文字((注意单个反斜杠用作字符串是通过逐字字符串文字来定义的,@“”)

  • \* - a literal *
  • \ * - 文字*

  • \s* - 0+ whitespaces (trimming the value from the left)
  • \ s * - 0+空格(修剪左边的值)

  • (.*?) - Group 1 capturing zero or more chars other than newline, as few as possible, up to the first occurrence of the subsequent subpatterns
  • (。*?) - 第1组捕获除换行之外的零个或多个字符,尽可能少,直到第一次出现的后续子模式

  • \s* - zero or more whitespaces (trimming from the right)
  • \ s * - 零个或多个空格(从右边修剪)

  • \* - a literal *
  • \ * - 文字*

  • \) - a literal )
  • \) - 文字)

To get the second value, you may use the same pattern, but add \s* (zero or more whitespaces) at the beginning, what is done with Regex.Replace(incomingValue, $@"\s*{pattern}", string.Empty).

要获得第二个值,您可以使用相同的模式,但在开头添加\ s *(零个或多个空格),使用Regex.Replace(incomingValue,$ @“\ s * {pattern}”,字符串完成的操作.Empty)。