Reputation: 1447
Hi I am newbie in RegEx operations. I have a text like
[JUNCTIONS]
;ID Elev Demand Pattern
3 50 100 ;
4 50 30 ;
5 50 20 ;
6 40 20 ;
7 50 5 ;
8 30 5 ;
9 30 5 ;
2 50 80 ;
10 50 70 ;
11 50 30 ;
12 50 52 ;
13 50 40 ;
14 50 40 ;
15 50 10 ;
16 50 10 ;
17 50 10 ;
18 0 0 ;
19 0 0 ;
[RESERVOIRS]
;ID Head Pattern
1 100 ;
[TANKS]
I want to create a pattern and output the text between [JUNCTIONS]
and [RESERVOIRS]
then [RESERVOIRS]
to [TANKS]
then so on. [XXXX] is not known to me. I want to get text inside [XXX] to [XXX]. How can i do that?
Upvotes: 0
Views: 127
Reputation: 19121
Why use a regex?
Assuming you can read this input text one line at a time, it will probably be quicker and easier to just loop over the lines, and output those you need. Some variant of:
Update:
In response to you comment below; you can probably use this to skip any lines with [something]
in them, and print out the rest:
// Pattern: Any instance of [] with one or more characters of between them:
var pattern = @"\[.+\]";
while((line = file.ReadLine()) != null)
{
if(!Regex.IsMatch(line, pattern)) // Skip lines that match
{
Console.WriteLine(line);
}
}
Upvotes: 1
Reputation: 1428
Here is some c# code to do the match, and get the results.
Be sure to add error checking, for example to make sure that the match actually worked.
Note the Singleline flag - this lets the dot (.) match all characters, including newlines. You'll also probably need to cleanup and trim the output, to remove any trailing newlines, etc.
MatchCollection matches = Regex.Matches(test, @"^\[JUNCTIONS\](.*)\[RESERVOIRS\](.*)\[TANKS\](.*)$", RegexOptions.Singleline);
GroupCollection groups = matches[0].Groups;
// JUNCTIONS text
Console.WriteLine(groups[1]);
// RESERVOIRS text
Console.WriteLine(groups[2]);
Edit - Updated to match OP's changes
If you want to match an unspecified number of matches, its a little trickier. This regex will match a [TEXT] block and anything that comes after it, until it its a [ character. The way to use this regex is to loop over the MatchCollection for each region, and use .groups[1] for the text and .groups[2] for the body.
MatchCollection matches =
Regex.Matches(test, @"\[([\w+]+)\]([^\[]+)?", RegexOptions.Singleline);
// for each block / section of the document
foreach(Match match in matches){
GroupCollection groups = match.Groups;
// [TEXT] part will be here
Console.WriteLine(groups[1]);
// The rest will be here
Console.WriteLine(groups[2]);
}
Upvotes: 1
Reputation: 39395
Here is the regex:
(?=(\[\S+\].*?\[\S+\]))
or
(?=(\[(?:JUNCTIONS|RESERVOIRS)\].*?\[(?:RESERVOIRS|TANKS)\]))
Assuming you want to handle all the [...]
things from your input.
Note: Use the make sure you are handling multiple line regex matching from your c#. And don't for get to escape the \
character if you need.
Upvotes: 1