Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parsing regex - (Not enough )'s

Tags:

c#

regex

I made a tiny database with books and trying to get titles, authors and year of book with regex in c# but error occured.

Database looks like this:

Eragon // Christopher Paolini // 2005

The Fellowship of the Ring // J. R. R. Tolkien // 1954

And code:

Regex r = new Regex(@"(?<title>(.*)//" +
                    @"(?<author>(.*)//" +
                    @"(?<year>(.*)$");

Error:

parsing "(?<tytul>(.*)//(?<autor>(.*)//(?<rok>(.*)$" - Not enough )'s.
like image 954
Dziki Arbuz Avatar asked Jan 09 '23 22:01

Dziki Arbuz


1 Answers

You forget to close all the named capturing groups.

@"(?<tytul>(.*))//(?<autor>(.*))//(?<rok>(.*))$"
               ^               ^             ^

DEMO

By turning the greedy quantifiers .* at the first to non-greedy .*? would avoid backtracking.

@"^(?<tytul>(.*?))//(?<autor>(.*?))//(?<rok>(.*))$"
like image 166
Avinash Raj Avatar answered Jan 14 '23 20:01

Avinash Raj