472,378 Members | 1,450 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

re.findall

A question about the findall function in the re module, and I also
would be happy with pointers to online documentation with which I
could have found a solution myself (if it even exists!).

I have this program and result:

-------------- program ------------------
import re

pat = r'(.*) or (.*)'
r = re.findall(pat, "i or j or k")
print r
-------------- result -------------------
[('i or j', 'k')]
-------------------------------------------

But the result I want is this:
[('i', 'j or k'), ('i or j', 'k')]

So my regular expression is wrong, but also variations like
r'^(.*|.*?) or (.*|.*?)$' do not work. I have the feeling I am
overlooking something simple. Maybe I should not try to use
re.findall to get the desired result, but do a re.match in a
while-loop?

Stated in English the problem is this: give me all combinations of
strings left and right of r' or ', and return them as a list of
tuples. Sure I can solve this without using the re module, using
while-loops and string.search etc., but I also have variations of
this problem and learning re seems useful.
Jul 18 '05 #1
1 3242
hw***@hotmail.com (Will Stuyvesant) wrote in
news:cb**************************@posting.google.c om:
A question about the findall function in the re module, and I also
would be happy with pointers to online documentation with which I
could have found a solution myself (if it even exists!).


The problem is that the strings you want to find are overlapping.
This should get you started:

import re

s = "i or j or k or grr"
pat = re.compile(r'\w+ or \w+')

startposition = 0
while 1:
res = pat.search(s, startposition)
if res == None:
break
startposition = res.start() + 1
print res.group()
Matthias
Jul 18 '05 #2

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

Similar topics

3
by: Michael Rockwell | last post by:
I am new to using C# generics and I am liking what I am finding. However the examples in online help are lacking. Can someone help me with the FindAll method of the generic List class? As I...
5
by: Dave | last post by:
Hi All C# ADSI samples that I run cause unspecified errors. If I translate them into VB.NET they run fine. Here's an example: public string getEmail(string LDAPPath, string username) {...
5
by: Mystilleef | last post by:
Hello, Is there a simple flag to set to allow overlapping matches for the findall() regular expression method? In other words, if a string contains five occurrences of the string pattern "cat",...
1
by: JerryB | last post by:
Hi, I have a string like this: invalidStr = "192.168.*.1" I want to be sure I don't get a * followed by a number, i.e. I want invalidStr to be invalid. So I do: numberAfterStar =...
3
by: DagoFlores | last post by:
Hi, let's take a look to this code that we already know.: List<stringdinosaurs = new List<string>(); dinosaurs.Add("Compsognathus"); dinosaurs.Add("Amargasaurus"); .... List<stringsublist =...
5
by: David Longnecker | last post by:
I'm working to create a base framework for our organization for web and client-side applications. The framework interfaces with several of our systems and provides the business and data layer...
7
by: Abhishek | last post by:
Hi I am using a generic list in my program to have a list of objects stored. I want to compare all the objects in this list with object of another class and find all the objects which meet the...
2
by: =?Utf-8?B?bWdvbnphbGVzMw==?= | last post by:
I have a List<tobject consisting of objects which in themselves consist of BindingListViews of objects. When I want to search for a object value I normally create a foreach loop and increment a...
1
by: Doug | last post by:
Hi, I have a collection of objects and I need to compare each item in the collection to see if it's a match on any or all of the others. I have stored my collection in an item like so: ...
2
by: Alexnb | last post by:
Okay, I am not sure if there is a better way of doing this than findAll() but that is how I am doing it right now. I am making an app that screen scapes dictionary.com for definitions. However, I...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...
0
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

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.