473,732 Members | 2,175 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 1373
<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
3063
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
10185
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
1983
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
2259
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
2116
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
2421
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
3109
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
9667
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
12199
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
5621
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
8773
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
9306
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...
1
9234
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9180
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
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
6030
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3259
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
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2177
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.