简体   繁体   English

正则表达式只获取双引号和单引号内的文本以及行号

[英]Regex to get only the text inside the double and Single quotes and the line number

Regex to get the text within the double and single quotes with line number. 正则表达式以获取带有行号的双引号和单引号内的文本。

For example: 例如:

line 123 Processing File "This is what i am looking for"<'this is also what i need'>

Output should be: 输出应为:

line 123 This is what i am looking for  this is also what i need

My regex is: 我的正则表达式是:

 MatchCollection matches2 = Regex.Matches(l, "\"([^\"]*)\"");

     foreach (Match match2 in matches2)
           {
                foreach (Capture capture2 in match2.Captures)
                    {
                        textBox4.Text = textBox4.Text + capture2.Value + Environment.NewLine;

                    }
            }

I am getting (my output): 我得到(我的输出):

"This is what i am looking for"

I don't need the double quotes, I need only the text within the quotes. 我不需要双引号,仅需要引号内的文本。

The first issue here is that you are looking at the match rather than the captures. 这里的第一个问题是您正在查看比赛而不是捕获。 The captures will show you the stuff gathered by the ( ) operators. 这些捕获将向您显示()运算符收集的内容。

matches2.OfType<Match>().Select(x => x.Captures[0].Value)

尝试这个:

Regex.Replace(inputtext, @"line (\d+).*?\""(.*?)""\W+(.*?)'", "line $1 $2 $3")
string input =
    "line 123 Processing File \"This is what i am looking for\"<'this is also what i need'>";
string output =
    string.Join(" ", Regex.Matches(input, @"(line\s+(\d+)|(""|').*?\3)")
                          .Cast<Match>()
                          .Select(m => Regex.Replace(m.Value,
                                                     @"^[""']|[""']$", "")));

Output: 输出:

line 123 This is what i am looking for this is also what i need

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

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