473,474 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how to use pyparsing for identifiers that start with a constant string

I am scanning text that has identifiers with a constant prefix string
followed by alphanumerics and underscores. I can't figure out, using
pyparsing, how to match for this. The example expression below seems to
be looking for whitespace between the 'atod' and the rest of the
identifier.

identifier_atod = 'atod' + pp.Word('_' + pp.alphanums)

How can I get pyparsing to match 'atodkj45k' and 'atod_asdfaw', but not
'atgdkasdjfhlksj' and 'atod asdf4er', where the first four characters
must be 'atod', and not followed by whitespace?

Thanks!

Jul 19 '05 #1
3 2232
ph*****************@yahoo.com wrote:
I am scanning text that has identifiers with a constant prefix string
followed by alphanumerics and underscores. I can't figure out, using
pyparsing, how to match for this. The example expression below seems to
be looking for whitespace between the 'atod' and the rest of the
identifier.

identifier_atod = 'atod' + pp.Word('_' + pp.alphanums)

How can I get pyparsing to match 'atodkj45k' and 'atod_asdfaw', but not
'atgdkasdjfhlksj' and 'atod asdf4er', where the first four characters
must be 'atod', and not followed by whitespace?


Here is one way using pyparsing.Combine:
from pyparsing import *
tests = [ 'atodkj45k', 'atod_asdfaw', 'atgdkasdjfhlksj', 'atod asdf4er']
ident = Combine(Literal('atod') + Word('_' + alphanums))
for t in tests: ... try:
... print ident.parseString(t)
... except:
... print 'No match', t
...
['atodkj45k']
['atod_asdfaw']
No match atgdkasdjfhlksj
No match atod asdf4er


Kent
Jul 19 '05 #2
Be careful, Kent. You may get tagged as "the new pyparsing guy." :)

-- Paul

Jul 19 '05 #3
Paul McGuire wrote:
Be careful, Kent. You may get tagged as "the new pyparsing guy." :)


Yeah, I was a little surprised I beat you to that one :-)

Kent
Jul 19 '05 #4

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

Similar topics

4
by: Bo¹tjan Jerko | last post by:
Hello ! I am trying to understand pyparsing. Here is a little test program to check Optional subclass: from pyparsing import Word,nums,Literal,Optional lbrack=Literal("").suppress()...
5
by: Lukas Holcik | last post by:
Hi everyone! How can I simply search text for regexps (lets say <a href="(.*?)">(.*?)</a>) and save all URLs(1) and link contents(2) in a dictionary { name : URL}? In a single pass if it could....
2
by: Peter Fein | last post by:
I'm trying to use pyparsing write a screenscraper. I've got some arbitrary HTML text I define as opener & closer. In between is the HTML data I want to extract. However, the data may contain the...
7
by: Steven Bethard | last post by:
How do I make sure that my entire string was parsed when I call a pyparsing element's parseString method? Here's a dramatically simplified version of my problem: py> import pyparsing as pp py>...
3
by: rh0dium | last post by:
Hi all, I have a file which I need to parse and I need to be able to break it down by sections. I know it's possible but I can't seem to figure this out. The sections are broken by <> with...
13
by: 7stud | last post by:
To the developer: 1) I went to the pyparsing wiki to download the pyparsing module and try it 2) At the wiki, there was no index entry in the table of contents for Downloads. After searching...
0
by: napolpie | last post by:
DISCUSSION IN USER nappie writes: Hello, I'm Peter and I'm new in python codying and I'm using parsying to extract data from one meteo Arpege file. This file is long file and it's composed by...
1
by: Neal Becker | last post by:
I'm just trying out pyparsing. I get stack overflow on my first try. Any help? #/usr/bin/python from pyparsing import Word, alphas, QuotedString, OneOrMore, delimitedList first_line = ''...
18
by: Just Another Victim of the Ambient Morality | last post by:
Is pyparsing really a recursive descent parser? I ask this because there are grammars it can't parse that my recursive descent parser would parse, should I have written one. For instance: ...
0
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,...
0
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,...
0
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...
0
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...
0
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
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...
0
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...

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.