473,320 Members | 2,117 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,320 software developers and data experts.

[import re] match or findall?

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:

import re
string = 'abc 123456 xyz'
if re.findall("\d{1,}",string):
print "Found a number" #Actually prints

Is there a function similar to findall that will find the 1st
occurrence?
I realize match is probably that similar function but I can't get this
simple
example working, for some reason.

anyone know what's wrong?

Thanks in advance.
-gohaku
Jul 18 '05 #1
1 3347
On Sat, 3 Jul 2004 18:02:45 -0400, gohaku <go****@earthlink.net> wrote:
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:

import re
string = 'abc 123456 xyz'
if re.findall("\d{1,}",string):
print "Found a number" #Actually prints

Is there a function similar to findall that will find the 1st
occurrence?
I realize match is probably that similar function but I can't get this
simple
example working, for some reason.


Match only matches at the beginning of the string, which is very different from
finding the first occurance.
match = re.search("\d{1,}",string)
if match is not None:
print "Found a number:", match.group()

--
Sam Holden
Jul 18 '05 #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\'...
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 =...
1
by: bjam | last post by:
Hi, today I was able to abstract out into a separate xsl file a template that I specifically perform a call templates for, this worked with import no problem. However, when trying to do the same...
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 =...
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...
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: Nick Craig-Wood | last post by:
I've been dumping a database in a python code format (for use with Python on S60 mobile phone actually) and I've noticed that it uses absolutely tons of memory as compared to how much the data...
1
by: Patrick C | last post by:
I'm importing a script that I made and it's literally take 10+mins to to run or import into PythonWin. I've put the script at the bottom. But i'm also having a problem with it. What i'm trying to...
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.