简体   繁体   English

如何编写从开始到出现新行匹配字符串的正则表达式?

[英]How to write regex that matches string from begining to first new line occurence?

how to match string from beginning to first new line feed. 如何从开始到第一个新换行符匹配字符串。

Example if TextBox1.Text value is 如果TextBox1.Text值为

'START: Milestone One\\r\\nFirst milestone is achieved in a week.' “ START:一个里程碑\\ r \\ n一个星期内就实现了第一个里程碑。”

Here is what I am trying:- 这是我正在尝试的:

TextBox1.Text = Regex.Replace(TestBox1.Text, @"START: \w+\s?", "");

This Regex is finding first word only therefore replacing 'START: MileStone' with string.empty but leaving 'One' there. 此正则表达式只找到第一个单词,因此将string的“ START:MileStone”替换为string.empty,而在其中保留“ One”。

I need to replace everything (including newline feed) before first newline feed. 我需要在第一个换行符之前替换所有内容(包括换行符)。

You do not need a regex for that, just use IndexOf('\\n') + 1 : 您不需要正则表达式,只需使用IndexOf('\\n') + 1

var t = "START: Milestone One\r\nFirst milestone is achieved in a week.";
Console.WriteLine(t.Substring(t.IndexOf('\n') + 1).Trim());

See IDEONE demo IDEONE演示

Result: First milestone is achieved in a week. 结果: First milestone is achieved in a week.

A regex will not be efficient here, but you can check it for yourself: 正则表达式在这里效率不高,但是您可以自己检查一下:

var text = Regex.Replace(t, @"(?>.+\r?\n)\s*", string.Empty);

See another demo 观看另一个演示

You can also use LINQ: 您也可以使用LINQ:

string str = "START: Milestone One\r\nFirst milestone is achieved in a week.";

string newStr = String.Join("", str.SkipWhile(c => c != '\n').Skip(1));

Console.WriteLine(newStr); // First milestone is achieved in a week.

To check if the input string is null or contains any \\n : 要检查输入字符串是否为null或包含任何\\n

string newStr = (str?.Any(c => c == '\n')).GetValueOrDefault() ? String.Join("", str.SkipWhile(c => c != '\n').Skip(1)) : str;

I don't know if your anything like me but maybe you are practicing Regex and want to know how to make it happen with it. 我不知道您是否喜欢我,但也许您正在练习Regex,并想知道如何实现它。 I'm very new at coding but I took your example and this is how I would make it happen. 我是编码方面的新手,但是我举了您的例子,这就是我要实现的方式。

    private void button1_Click(object sender, EventArgs e)
    {
        string test = "START: Milestone One\r\nFirst milestone is achieved in a week.";

        string final = Regex.Replace(test, ".*\\n", "START: ").ToString();
        MessageBox.Show(final); // START: First milestone is achieved in a week.
    }

Not sure if that helps your or not but thought I would try. 不确定是否对您有帮助,但我想我会尝试。 Have a nice day! 祝你今天愉快!

Just fixing your regex: 只需修复您的正则表达式:

[\w\s]+\n

you might need to play with \\n and \\ra bit. 您可能需要使用\\ n和\\ ra位。 [\\w\\s] is equivalent to . [\\ w \\ s]等效于。 so .*\\n will do the job as well 所以.*\\n也可以做

Yours didn't work as you stopped at first whitespace, while you want to replace any word or whitespace until the linefeed, as far as I can understand. 据我所知,您在第一个空格处停下来时无法工作,而您想在换行之前替换任何单词或空格。

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

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