Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression matching from the end

Tags:

c#

regex

start123start123

start123endstart345end

start567endstart789end

I need to extract a number of sets of data(bold) between all start and end of the above string.

My code:

Regex re = new Regex(start(.*)end, RegexOptions.Singleline);
foreach (Match m in re.Matches(text)) dosomething();

The only extracted text will be 789

The problem is that I dont know the exact number of start and end formatted text needed to be extract. I want my regular expression to be able to ignore the start first two start only but greedy regex is ignoring all the start until the last one.

Can it be stopped after it matches the first end text?

If not, is there an option to matched the text from the back?

Update:

Actually, my original code is using non-greedy regex.

The extracted text will be 123start123\r\nstart123 , 345 , 567 , 789

The newline parameter RegexOptions.Singleline is necessary in my real case, I am simplifying the case here for everyone to understand easily

Update 2:

My expected output is 123 , 345 , 567 , 789

like image 745
Isolet Chan Avatar asked Jul 11 '26 05:07

Isolet Chan


2 Answers

The * is a greedy operator. Therefore, .* will match as much as it can and still allow the remainder of the regular expression to match.To get a non-greedy match, use *?

start(.*?)end

Edit

If I understand your problem correctly, you can use a Negative Lookahead. ( Explanation )

String s = @"start123start123
start123endstart345end
start567endstart789end";

Regex re = new Regex(@"(?s)start((?:(?!start).)*)end");

foreach (Match m in re.Matches(s))
         Console.WriteLine(m.Groups[1].Value);

Output

123
345
567
789
like image 135
hwnd Avatar answered Jul 13 '26 19:07

hwnd


If you need to get only the numbers between start and end excluding the words start & end ofcourse:

Regex reg = new Regex(@"(?<=start)[0-9]*(?=end)");
string test = "start123endstart345end";
var resultings = reg.Matches(test);

It will get {1,2,3} {3,4,5} {5,6,7} {7,8,9} in the string you showed:

start123endstart345end

start567endstart789end
like image 43
terrybozzio Avatar answered Jul 13 '26 17:07

terrybozzio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!