473,387 Members | 1,592 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,387 software developers and data experts.

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(invalidStr):
print "Found it with findall!"

it prints, if I run:

if numberAfterStar.match(invalidStr):
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 1846
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(invalidStr):
print "Found it with findall!"

it prints, if I run:

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

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


This might help:
numberAfterStar.findall(invalidStr) ['*.1'] numberAfterStar.match(invalidStr)
numberAfterStar.search(invalidStr)

<_sre.SRE_Match 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
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\'...
1
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
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 =...
4
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...
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...
32
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...
10
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. ...
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...
8
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....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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...

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.