Reputation: 1
I have exhausted my search and need asisstance. I am new to regex and have managed to pull words from a multi lined string, however not a whole line. I have text pulled into string but I cannot find out to grab the next line.
Example string has multiple lines (string multipleLines):
Authentication information
User information:
domai n\username
Paris
I need to grab the text "domain\username" after the line "User iformation."
I have tried many combinations of regex and cannot get it to work. Example:
string topLine = "Authentication information";
label.Text = Regex.Match(multipleLines, topLine + "(.*)").Groups[1].Value;
I also tried using: topLine + "\n" What should I add to look at the entire next line after getting the line for Authentication information?
Upvotes: 0
Views: 1960
Reputation: 11478
Your objective with Regular Expressions can be found here at this thread on Stack Overflow. You would want to implement the RegexOptions.Multiline
so you can make usage of the ^
and $
to match the Start and End of a line.
^
: Depending on whether the MultiLine option is set, matches the position before the first character in a line, or the first character in the string.
$
: Depending on whether the MultiLine option is set, matches the position after the last character in a line, or the last character in the string.
Those would be the easiest way to accomplish your task.
Update:
An example would be something like this.
const string account = @"Authentication information:" + "\n"
+ "User Information: " + "\n"
+ "Domain Username: "
+ " \n" + "\\Paris";
MatchCollection match = Regex.Matches(account, ^\\.*$, RegexOptions.Multiline);
That will retrieve the line with the \\
and all that proceed it on that line. That is an example, hopefully that points you in the correct direction.
Upvotes: 1
Reputation: 48736
Though RegEx would accomplish what you want, this might be simpler and far less overhead. Using this code depends on the kind of input you'll be receiving. For your example, this will work:
string domainUsername = inputString.Split('\n').Where(z => z.ToLower().Contains(@"\")).FirstOrDefault();
if (domainUsername != null) {
Console.WriteLine(domainUsername); // Should spit out the appropriate line.
} else {
Console.WriteLine("Domain and username not found!"); // Line not found
}
Upvotes: 0