I have a string like this:
string text = "ext_bus 0 0/0/3/0.0 side CLAIMED INTERFACE IDE Primary Channel\r\ntarget 0 0/0/3/0.0.0 tgt CLAIMED DEVICE \r\ndisk 0 0/0/3/0.0.0.0 sdisk";
When I do a regex multiline search to get the text in ext_bus third column (0/0/3/0.0) and last column (IDE Primary Channel):
Regex regExp = new Regex(@"^ext_bus\s*[0-9]+\s*(?<HWPath>\S+).*\s{2,}(?<BusName>.*?)\r?$", RegexOptions.Multiline);
The first group is OK: "0/0/3/0.0"
But the second group is the next line!: "target 0 0/0/3/0.0.0 tgt CLAIMED DEVICE "
How can this be possible with Multiline (only one line), and how can I get the last column (the text at the end of the string after 2 or more whitespaces).
The short answer is that it is because the first .*
in your regex matches up till the end of the first line, then the \\s{2,}
matches the newline characters, then the (?<BusName>.*?)
will match all of the second line.
Multiline
mode means that ^
and $
match the start and end of a line, not just the start and end of the whole string.
Remove the .*
and then <BusName>
will be the rest of the text on the line after the whitespace following 0/0/3/0.0
.
Why do you use regex?
You can do it easily with split
string value = "ext_bus 0 0/0/3/0.0 side CLAIMED INTERFACE IDE Primary Channel\r\ntarget 0 0/0/3/0.0.0 tgt CLAIMED DEVICE \r\ndisk 0 0/0/3/0.0.0.0 sdisk";
char[] delimiters = new char[] {' ' }; // here you can add more seperaors
string[] parts = value.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < parts.Length; i++)
{
Console.WriteLine(parts[i]);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.