473,698 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regex at Index == 0

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
Sep 23 '08 #1
5 1367
<je**********@g mail.comwrote in message
news:f1******** *************** ***********@f36 g2000hsa.google groups.com...
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)|(exp r2)|...|(exprN) )

Also are you tokenising? E.g., a form of command and value list syntax?
--
Anthony Jones - MVP ASP/ASP.NET

Sep 23 '08 #2
je**********@gm ail.com wrote:
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 ^.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Sep 23 '08 #3
On Sep 23, 8:02*am, "Anthony Jones" <AnthonyWJo...@ yadayadayada.co m>
wrote:
<jehugalea...@g mail.comwrote in message

news:f1******** *************** ***********@f36 g2000hsa.google groups.com...


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)|(exp r2)|...|(exprN) )

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

--
Anthony Jones - MVP ASP/ASP.NET- Hide quoted text -

- Show quoted text -
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.
Sep 23 '08 #4
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.Create Query<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
Sep 23 '08 #5


<je**********@g mail.comwrote in message
news:ed******** *************** ***********@i76 g2000hsf.google groups.com...
>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.Create Query<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
Sep 24 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3060
by: Tap | last post by:
I would like to parse the following string with as little code as possible. stringValue = "Email Message ID:TAPASVI to tapsbin@hotmail.com;tapasbin@cs.com;{CC}tapstemp@hotmail.com;{CC}tapstemp007@hotmail.com" 1) Take everything in between "ID:" and the word " to" i.e. stringVal1 = "TAPASVI" 2) Take between the word " to " and "{CC}" i.e. stringVal2 = "tapsbin@hotmail.com;tapasbin@cs.com;"
5
10180
by: JackRazz | last post by:
Anyone know the regular expression to match a blank line where the byte sequence is "0D 0A 0D 0A" ive tried "\r\n\r\n+", "^$+" "\n\r" with no success. Any Ideas? Thanks - JackRazz This is the code fragment I'm trying Dim r As Regex, m As Match, i As Integer
8
1980
by: Just Me | last post by:
I want to use regular expressions to search a string, give the user the option of replacing, and then maybe replacing the data - using reg expressions for the search and the replace strings. However all the Regex replace methods seem to combine in one call the search and replace. Is there a way of doing what I want?
7
2257
by: lgbjr | last post by:
Hi All, I'm trying to split a string on every character. The string happens to be a representation of a hex number. So, my regex expression is (). Seems simple, but for some reason, I'm not getting the results I expect. Dim SA as string() Dim S as string S="FBE"
3
2114
by: jg | last post by:
I made a mistake somewhere in my vb code and I look, check and read against the articles and help on regex, I still can't find the mistake I made. I know my test string and the test patterns works, because I used on a vs. script to check. I also believe I foolwed followed the regex syntax for dotnet. here is the source code for the function and testing Public Function regtest(ByVal StringIn As String, ByVal patrn As
5
2420
by: Kofi | last post by:
Any takers? Got a string of DNA as an input sequence GGATGGATG, apply the simple regex "GGATG" as in Regex r = new Regex("GGATG", (RegexOptions.Compiled)); MatchCollection matches = r.Matches("GGATGGATG"); Now I would expect to get two matches right? One at index 0 in the
11
3101
by: Steve | last post by:
Hi All, I'm having a tough time converting the following regex.compile patterns into the new re.compile format. There is also a differences in the regsub.sub() vs. re.sub() Could anyone lend a hand? import regsub
6
9666
by: Gary Bond | last post by:
Hi All, Being a bit of a newbie with regex, I am confused when using word boundaries. For instance, I want to replace all the stand alone '.5k' that occur in an input string, with 500. In other words "this is a .5k example" goes to "this is a 500 example" The replace should not touch '.5k' that occurs inside a word. For example:
1
12190
by: jonnyboy6969 | last post by:
Hi All Really hoping someone can help me out here with my deficient regex skills :) I have a function which takes a string of HTML and replaces a term (word or phrase) with a link. The pupose is that I seek out terms which are in a glossary on our site, and automatically link to this definition. Its slightly complex becase certain elements have to be ignored, for exampleI dont want to add links within existing links, or for example link...
1
5620
by: al.moorthi | last post by:
the below program is working in Suse and not working on Cent 5: can any body have the solution ? #include <regex.h> #include <stdlib.h> #include <stdio.h> int main(){ char cool = "http://www.cnn.com:80/wowsers.html";
0
8675
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9029
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7729
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2331
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.