473,396 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Pattern Matching

Hello-

I'm running Python 2.2.3 on Windows XP "Professional" and am reading a file
wit 1 very long line of text (the line consists of multiple records with no
cr/lf). What I would like to do is scan for the occurrence of a specific
pattern of characters which I expect to repeat many times in the file.
Suppose I want to search for "Start: mm/dd/yy" and capture the mm/dd/yyyy
data for processing each time I find it. This is the type of problem I used
to solve with <duck>Perl<\duck> in a former lifetime using regular
expressions. The following does not work, but is the flavor of what I want
to do:

long_line_of_text = 'Start: 1/1/2004 and some stuff.~Start: 2/3/2004 stuff.
~Start 5/1/2004 morestuff.~'
while re.match('Start:\ (\D?/\D?/\D+)', long_line_of_text):
# process the date string here which I hoped to catch in the parenthesis
above.

I'd like this to keep matching and processing the string as long as it keeps
matching the pattern, bopping down the string as it goes.

Another way to handle this is to replace all of the tildes with linefeeds
(tildes are the end of segment marker), or split the records on the tilde
and go from there. I'd just like to know how I could do it with the regular
expressions.

Thanks for your help,
--greg

Greg Lindstrom (501) 975-4859
NovaSys Health gr************@novasyshealth.com

"We are the music makers, and we are the dreamers of dreams" W.W.
Jul 18 '05 #1
3 1727
On Mon, 19 Jul 2004, Greg Lindstrom wrote:
The following does not work, but is the flavor of what I want to do:

long_line_of_text = 'Start: 1/1/2004 and some stuff.~Start: 2/3/2004 stuff.
~Start 5/1/2004 morestuff.~'
while re.match('Start:\ (\D?/\D?/\D+)', long_line_of_text):
# process the date string here which I hoped to catch in the parenthesis
above.

I'd like this to keep matching and processing the string as long as it keeps
matching the pattern, bopping down the string as it goes.


That line tastes distincly Perlish ;)

What you want to write in Python is:

for match in re.finditer('Start:\ (\D?/\D?/\D+)', long_line_of_text):
<do something with match.group(1)>

re.finditer() returns an iterator that loops over all occurances of the
pattern in the string, returning a match object for each one.
match.group() returns the actual text of the match, and match.group(n)
returns the text of group n.

I'm curious, though, why do you escape the space? My guess is it's
something from Perl that I don't remember.

Jul 18 '05 #2
Greg Lindstrom <gr************@novasyshealth.com> wrote:
long_line_of_text = 'Start: 1/1/2004 and some stuff.~Start: 2/3/2004 stuff.
~Start 5/1/2004 morestuff.~'
while re.match('Start:\ (\D?/\D?/\D+)', long_line_of_text):
# process the date string here which I hoped to catch in the parenthesis
above.

I'd like this to keep matching and processing the string as long as it keeps
matching the pattern, bopping down the string as it goes.


p = re.compile(your_pattern_from_above)
matches = p.findall(long_line_of_text)

matches will be a list of your matches caught in the parenthesis

--
Kristofer Pettijohn
kr*******@cybernetik.net
Jul 18 '05 #3
"Greg Lindstrom" <gr************@novasyshealth.com> writes:
Hello- I'm running Python 2.2.3 on Windows XP "Professional" and am reading a file
wit 1 very long line of text (the line consists of multiple records with no
cr/lf). What I would like to do is scan for the occurrence of a specific
pattern of characters which I expect to repeat many times in the file.
Suppose I want to search for "Start: mm/dd/yy" and capture the mm/dd/yyyy
data for processing each time I find it. This is the type of problem I used
to solve with <duck>Perl<\duck> in a former lifetime using regular
expressions. The following does not work, but is the flavor of what I want
to do: long_line_of_text = 'Start: 1/1/2004 and some stuff.~Start: 2/3/2004 stuff.
~Start 5/1/2004 morestuff.~'
while re.match('Start:\ (\D?/\D?/\D+)', long_line_of_text):
# process the date string here which I hoped to catch in the parenthesis
above. I'd like this to keep matching and processing the string as long as it keeps
matching the pattern, bopping down the string as it goes. Another way to handle this is to replace all of the tildes with linefeeds
(tildes are the end of segment marker), or split the records on the tilde
and go from there. I'd just like to know how I could do it with the regular
expressions.


In addition to previous answers, a useful resource might be:
http://gnosis.cx/TPiP/
Jul 18 '05 #4

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

Similar topics

8
by: gsv2com | last post by:
One of my weaknesses has always been pattern matching. Something I definitely need to study up on and maybe you guys can give me a pointer here. I'm looking to remove all of this code and just...
176
by: Thomas Reichelt | last post by:
Moin, short question: is there any language combining the syntax, flexibility and great programming experience of Python with static typing? Is there a project to add static typing to Python? ...
9
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # Matching string patterns # # Sometimes you want to know if a string is of # particular pattern. Let's say in your website # you have converted all images...
1
by: Henry | last post by:
I have a table that stores a list of zip codes using a varchar column type, and I need to perform some string prefix pattern matching search. Let's say that I have the columns: 94000-1235 94001...
10
by: bpontius | last post by:
The GES Algorithm A Surprisingly Simple Algorithm for Parallel Pattern Matching "Partially because the best algorithms presented in the literature are difficult to understand and to implement,...
5
by: olaufr | last post by:
Hi, I'd need to perform simple pattern matching within a string using a list of possible patterns. For example, I want to know if the substring starting at position n matches any of the string I...
9
by: Jim Lewis | last post by:
Anyone have experience with string pattern matching? I need a fast way to match variables to strings. Example: string - variables ============ abcaaab - xyz abca - xy eeabcac - vxw x...
2
by: Ole Nielsby | last post by:
First, bear with my xpost. This goes to comp.lang.c++ comp.lang.functional with follow-up to comp.lang.c++ - I want to discuss an aspect of using C++ to implement a functional language, and...
1
by: VanKha | last post by:
I write this program for pattern-matching,but it gives wrong result: #include<iostream> #include<conio.h> #include<string.h> using namespace std; main() { char text,pat;...
5
by: pramodkh | last post by:
Hi All I am trying to match a pattern in a file and insert a line. If the pattern matches then insert a line before the matching pattern line. for example, I have the following content in a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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...

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.