0

I want to save an e-mail-address out of a .txt-file into a string variable. This is my code:

String path = "C:\\Users\\test.txt";
string from;

var fro = new Regex("from: (?<fr>)");

using (var reader = new StreamReader(File.OpenRead(@path)))
{
    while (true)
    {
        var nextLine = reader.ReadLine();
        if (nextLine == null)
            break;

        var matchb = fro.Match(nextLine);

        if (matchb.Success)
        {
            from = matchb.Groups["fr"].Value;
            Console.WriteLine(from);
        }
    }
}

I know that matchb.Success is true, however from won't be displayed correctly. I'm afraid it has something to do with the escape sequence, but I was unable to find anything helpful on the internet.

The textfile might look like this:

LOG 00:01:05 processID=123456-12345 from: test@test.org
LOG 00:01:06 processID=123456-12345 OK

Hille
  • 2,123
  • 22
  • 39
  • 3
    Looks like you forgot to define the named group pattern, try `"from: (?.*)"` if you need to match all the rest of the line after `from:`. If the email is just 1 or more non-whitespace, you may use `\S+`. To make sure there is `@`, you may use `\S+@\S+` instead of `.*` (in code, use `@"from: *(?\S+@\S+)"`). – Wiktor Stribiżew Jan 05 '18 at 10:31
  • 3
    You should never use a [c# keyword](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/from-clause) as variable name. – Tobias Theel Jan 05 '18 at 10:32
  • 3
    @TobiasTheel: It's only a *contextual* keyword, and it may well be the most appropriate variable name in many cases. For contextual keywords from query expressions, I'd try to avoid using variables named that way *in query expressions* but otherwise I don't think it's a significant problem. – Jon Skeet Jan 05 '18 at 10:34
  • https://stackoverflow.com/questions/48019345/find-all-email-addresses-in-a-string-using-regex-in-c-sharp/48019523#48019523 – pmcilreavy Jan 05 '18 at 10:38
  • @JonSkeet: I know, that was only ment as a general hint on c# keywords and not as a solution to the problem. – Tobias Theel Jan 05 '18 at 10:55
  • 1
    @TobiasTheel: But the OP hasn't *got* any variables named after C# keywords. He's got a variable with the same name as a *contextual* keyword, and he's not using that in a query expression, so I don't see a problem with it. – Jon Skeet Jan 05 '18 at 10:56
  • @JonSkeet: I'd never challenge your wisdom. I agree, that this is not a problem. – Tobias Theel Jan 05 '18 at 11:46

1 Answers1

2

Your (?<fr>) pattern defines a named group "fr" that matches an empty string.

To fill the group with some value you need to define the group pattern.

If you plan to match the rest of the line, you may use .*. To match a sequence of non-whitespace chars, use \S+. To match a sequence of non-whitespace chars that has a @ inside, use \S+@\S+. All the three approaches will work for the current scenario.

In C#, it will look like

var fro = new Regex(@"from: *(?<fr>\S+@\S+)");

Note that @"..." is a verbatim string literal where a single backslash defines a literal backslash, so you do not have to double it. I also suggest using the * quantifier to match 0 or more spaces before the email. You might want to use \s* (to match any 0+ whitespace chars) or [\p{Zs}\t]* (to match only horizontal whitespace chars) instead.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563