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

Home Posts Topics Members FAQ

re.match versus re.findall

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 = re.compile(r'\* .*\d')

Now, here's the fun:

If I run:
if numberAfterStar .findall(invali dStr):
print "Found it with findall!"

it prints, if I run:

if numberAfterStar .match(invalidS tr):
print "Found it with match!"

it doesn't.
Why does findall finds a match it but match doesn't?

Thanks!
Jerry

Feb 9 '06 #1
1 1858
JerryB wrote:
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 = re.compile(r'\* .*\d')

Now, here's the fun:

If I run:
if numberAfterStar .findall(invali dStr):
print "Found it with findall!"

it prints, if I run:

if numberAfterStar .match(invalidS tr):
print "Found it with match!"

it doesn't.
Why does findall finds a match it but match doesn't?


This might help:
numberAfterStar .findall(invali dStr) ['*.1'] numberAfterStar .match(invalidS tr)
numberAfterStar .search(invalid Str)

<_sre.SRE_Mat ch object at 0x00AF3DB0>

Check the docs on match, specifically where it talks about when you
should use search() instead... (http://docs.python.org/lib/re-objects.html)

(By the way, thank you for the excellently crafted post. I wish
everyone who asked questions here took the time to prepare as thoroughly
and include the actual failing code, etc...)

-Peter

Feb 10 '06 #2

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

Similar topics

2
1583
by: Sunit Joshi | last post by:
Hello All I have a problem here where I need to find the drive letters in a text file and replace them with another letter. Now the lines can be as long as this 6 'C:\pds\bddin64\report\mto\' 'SJOSHI' 'D:\pds\bddin64\report\format\' 'SJOSHI' with multiple entries for path; i.e. there may be 2, as above or 1 or more then 2. If I use the expression .*'(:).*?', it only matches
1
3363
by: gohaku | last post by:
Hi everyone, I am having a problem with the match function: import re string = 'abc 123456 xyz' if re.match("\d{1,}",string): print "Found a number" #Does not print whereas findall works:
4
1647
by: ajikoe | last post by:
Hello, For example I have a string : "Halo by by by" Then I want to take and know the possition of every "by" how can I do it in python? I tried to use: p = re.compile(r"by") m = p.search("Helo by by by")
4
2148
by: Johnny Lee | last post by:
Hi, I've met a problem in match a regular expression in python. Hope any of you could help me. Here are the details: I have many tags like this: xxx<a href="http://xxx.xxx.xxx" xxx>xxx xxx<a href="wap://xxx.xxx.xxx" xxx>xxx xxx<a href="http://xxx.xxx.xxx" xxx>xxx ..... And I want to find all the "http://xxx.xxx.xxx" out, so I do it
3
31443
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 understand the method, it will return a list that meets the criteria evaluated in the delegate function that I use for evaluation. I am not sure how to write this delegate, how do I get the list item to evaluate in my delegate. Is their a defined...
32
14671
by: Licheng Fang | last post by:
Basically, the problem is this: 'do' Python's NFA regexp engine trys only the first option, and happily rests on that. There's another example: 'oneself' The Python regular expression engine doesn't exaust all the
10
2944
by: wo_shi_big_stomach | last post by:
Newbie to python writing a script to recurse a directory tree and delete the first line of a file if it contains a given string. I get the same error on a Mac running OS X 10.4.8 and FreeBSD 6.1. Here's the script: # start of program # p.pl - fix broken SMTP headers in email files #
2
2251
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 counter to provide me a count. How can I refactor the same function w/o the foreach and use findall instead? faux data structure: --------------------- -> | Customers
8
5992
by: The Web President | last post by:
Dear all, this is really driving me nuts and any help would be extremely appreciated. I have a string that contains some numeric data. I want to isolate these data using re.match, as follows. bogus = "IFC(35m)" data = re.match(r'(\d+)',bogus)
0
8603
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
9157
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...
1
8893
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
7721
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...
1
6518
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
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();...
0
4615
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2327
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1999
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.