Regex at Index == 0

J

jehugaleahsa

Hello:

I have an input string that is supposed to follow a particular syntax.
I am making a simple parser. In doing so, I need to write a lexical
analyzer. I have decided to implement it in terms of regular
expressions.

I want to take a list of regular expressions and iterate over them,
finding the first one that matches. Of course, it has to match AND the
match must start at index == 0.

The problem I have is that the regex has to look through the whole
string. I want to limit it to being the first position. Would
appending ^ to every regex work? I am just looking for a way to short-
circuit.

Thanks,
Travis
 
A

Anthony Jones

Hello:

I have an input string that is supposed to follow a particular syntax.
I am making a simple parser. In doing so, I need to write a lexical
analyzer. I have decided to implement it in terms of regular
expressions.

I want to take a list of regular expressions and iterate over them,
finding the first one that matches. Of course, it has to match AND the
match must start at index == 0.

The problem I have is that the regex has to look through the whole
string. I want to limit it to being the first position. Would
appending ^ to every regex work? I am just looking for a way to short-
circuit.

Yes ^ would require that the pattern match only if the first character in
the search string matches the first character in the expression.

How many RegExps are you using, if just a few had you considered using a
single RegExp:-

^(?:(exp1)|(expr2)|...|(exprN))

Also are you tokenising? E.g., a form of command and value list syntax?
 
M

Martin Honnen

I want to take a list of regular expressions and iterate over them,
finding the first one that matches. Of course, it has to match AND the
match must start at index == 0.

The problem I have is that the regex has to look through the whole
string. I want to limit it to being the first position. Would
appending ^ to every regex work? I am just looking for a way to short-
circuit.

If you want to ensure that your regular expression matches only from the
beginning of the string then you indeed need to prefix your regular
expression with ^.
 
J

jehugaleahsa

Yes ^ would require that the pattern match only if the first character in
the search string matches the first character in the expression.

How many RegExps are you using, if just a few had you considered using a
single RegExp:-

^(?:(exp1)|(expr2)|...|(exprN))

Also are you tokenising? E.g., a form of command and value list syntax?

Yes, I am tokenizing. It is a syntax similar to a SQL where clause. It
seems simple enough. I have to know what type of token I have found. I
have that covered already with my token class. It tells me the token
type and the actual value. Hopefully this will work out smoothly.
 
J

jehugaleahsa

Yes, I am tokenizing. It is a syntax similar to a SQL where clause. It
seems simple enough. I have to know what type of token I have found. I
have that covered already with my token class. It tells me the token
type and the actual value. Hopefully this will work out smoothly.- Hide quoted text -

- Show quoted text -

Awesome! I got my parser working. The intent of the code was to allow
my database library to generate the WHERE clause of a SQL statement by
interpretting conditions on properties of my business objects.

So, say I have a Customer class with properties that map to a database
table. My library has features for mapping between database tables and
classes via XML or Attributes (similar to NHibernate or LINQ). I
wanted to allow the users of my library to get away from SQL
completely. So, they can now say something like this:

foreach (Customer customer in registry.CreateQuery<Customer>(
"(Name = {0} AND State = {1}) OR Type = {2}",
"Microsoft", "WA", "Preferred"))
{
}

My code will generate database-specific SQL with the actual table name
and column names used instead of the class and property names.

The cool thing now is that I don't need to write SQL anymore and so my
code becomes a little more database independent. I generate my classes
and attributes/mapping files using a tool I wrote too, so I don't have
to worry about writing that either.

DB development just became a whole lot easier for my users. Yay!

Thanks for everyone's input.

~Travis
 
M

Mythran

Awesome! I got my parser working. The intent of the code was to allow
my database library to generate the WHERE clause of a SQL statement by
interpretting conditions on properties of my business objects.

So, say I have a Customer class with properties that map to a database
table. My library has features for mapping between database tables and
classes via XML or Attributes (similar to NHibernate or LINQ). I
wanted to allow the users of my library to get away from SQL
completely. So, they can now say something like this:

foreach (Customer customer in registry.CreateQuery<Customer>(
"(Name = {0} AND State = {1}) OR Type = {2}",
"Microsoft", "WA", "Preferred"))
{
}

My code will generate database-specific SQL with the actual table name
and column names used instead of the class and property names.

The cool thing now is that I don't need to write SQL anymore and so my
code becomes a little more database independent. I generate my classes
and attributes/mapping files using a tool I wrote too, so I don't have
to worry about writing that either.

DB development just became a whole lot easier for my users. Yay!

Thanks for everyone's input.

~Travis

Hmm....make sure you are protecting against injection attacks....

Mythran
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Newbie question about Regex 8
Regex help 1
C# and regex issue 7
simple Regex does not match 2
Regex help needed 1
Regular expressions in C# 5
Regular Expressions Question 8
regex replace question 1

Top