473,698 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

re.match and non-alphanumeric characters

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)
print data.group(1)

I would expect to have "35" printed out to screen, but instead I get
an error that the regular expression did not match:

Traceback (most recent call last):
File "C:\Documen ts and Settings\Mattia \Desktop\Neeltj e\read.py",
line 20, in <module>
print data.group(1)
AttributeError: 'NoneType' object has no attribute 'group'

Note that the same holds if I look for "35" straight, instead of "\d
+". If instead I look for "IFC" it works fine. That is, apparently
re.match will match only up to the first non-alphanumeric character
and ignore anything after a "(", "_", "[" and god knows what else.

I am using Python 2.6 (r26:66721, latest stable version). Am I missing
something very big and very important?
Nov 16 '08 #1
8 5991
r
On Nov 16, 10:33*am, The Web President <mattia.land... @gmail.com>
wrote:
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)
print data.group(1)

I would expect to have "35" printed out to screen, but instead I get
an error that the regular expression did not match:

Traceback (most recent call last):
* File "C:\Documen ts and Settings\Mattia \Desktop\Neeltj e\read.py",
line 20, in <module>
* * print data.group(1)
AttributeError: 'NoneType' object has no attribute 'group'

Note that the same holds if I look for "35" straight, instead of "\d
+". If instead I look for "IFC" it works fine. That is, apparently
re.match will match only up to the first non-alphanumeric character
and ignore anything after a "(", "_", "[" and god knows what else.

I am using Python 2.6 (r26:66721, latest stable version). Am I missing
something very big and very important?
try re.search or re.findall
re.match is only at the beginning of a string
i almost never use it
>>re.search('(\ d+)', bogus).group()
'35'
>>re.search('(\ d+)', bogus).span()
(4, 6)
Nov 16 '08 #2
On Nov 16, 4:33*pm, The Web President <mattia.land... @gmail.com>
wrote:
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)
print data.group(1)

I would expect to have "35" printed out to screen, but instead I get
an error that the regular expression did not match:

Traceback (most recent call last):
* File "C:\Documen ts and Settings\Mattia \Desktop\Neeltj e\read.py",
line 20, in <module>
* * print data.group(1)
AttributeError: 'NoneType' object has no attribute 'group'

Note that the same holds if I look for "35" straight, instead of "\d
+". If instead I look for "IFC" it works fine. That is, apparently
re.match will match only up to the first non-alphanumeric character
and ignore anything after a "(", "_", "[" and god knows what else.

I am using Python 2.6 (r26:66721, latest stable version). Am I missing
something very big and very important?
re.match() anchors the match at the start of the string. What you need
is re.search(). It's all in the documentation! :-)
Nov 16 '08 #3
En Sun, 16 Nov 2008 14:33:42 -0200, The Web President
<ma************ @gmail.comescri bió:
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)
print data.group(1)

I would expect to have "35" printed out to screen, but instead I get
an error that the regular expression did not match:
http://docs.python.org/library/re.ht...g-vs-searching

--
Gabriel Genellina

Nov 16 '08 #4
The Web President wrote:
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)
print data.group(1)

I would expect to have "35" printed out to screen, but instead I get
an error that the regular expression did not match:

Traceback (most recent call last):
File "C:\Documen ts and Settings\Mattia \Desktop\Neeltj e\read.py",
line 20, in <module>
print data.group(1)
AttributeError: 'NoneType' object has no attribute 'group'

Note that the same holds if I look for "35" straight, instead of "\d
+". If instead I look for "IFC" it works fine. That is, apparently
re.match will match only up to the first non-alphanumeric character
and ignore anything after a "(", "_", "[" and god knows what else.

I am using Python 2.6 (r26:66721, latest stable version). Am I missing
something very big and very important?
Yep - re.search. Match matches the whole string. You want searching.
Diez
Nov 16 '08 #5
On Nov 17, 4:44*am, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
Match matches the whole string.
*ONLY* if the pattern ends with "$" or r"\Z"
Nov 16 '08 #6
John Machin schrieb:
On Nov 17, 4:44 am, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
> Match matches the whole string.

*ONLY* if the pattern ends with "$" or r"\Z"

You think so?

import re

rex = re.compile("abc .*def")

if rex.match("abc0 123455678def"):
print "matched"

Diez
Nov 16 '08 #7
Diez B. Roggisch wrote:
John Machin schrieb:
>On Nov 17, 4:44 am, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
>> Match matches the whole string.

*ONLY* if the pattern ends with "$" or r"\Z"


You think so?

import re

rex = re.compile("abc .*def")

if rex.match("abc0 123455678def"):
print "matched"
Your test is inconclusive: necessary, but not sufficient.
>>rex = re.compile("abc .*def")

if rex.match("abc0 123455678defPLU SEXTRASTUFF"):
.... print "Matched"
....
Matched
>>>
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Nov 16 '08 #8
On Nov 17, 10:19*am, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
John Machin schrieb:
On Nov 17, 4:44 am, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
*Match matches the whole string.
*ONLY* if the pattern ends with "$" or r"\Z"

You think so?

import re

rex = re.compile("abc .*def")

if rex.match("abc0 123455678def"):
* * *print "matched"
OK, I'll try again:

The following 3-tuples represent (pattern, string,
matched_portion _of_string):
('abc', 'abc', 'abc')
('abc', 'abcdef', 'abc')
('abc$', 'abc', 'abc')
('abc$', 'abcdef', '<no match>')

Saying "Match matches the whole string" is incorrect; see the second
case. If you want to ensure that the whole string matches the pattern,
the pattern needs to be terminated by "$" or "\Z".
Nov 17 '08 #9

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

Similar topics

2
1644
by: Victor | last post by:
While debugging my stylesheet using XMLSpy, I got a really weird result: <xsl:if test="$match = true()"> matches even if $match is an empty node fragment. How can this be? -- Victor
3
18749
by: Derek Stone | last post by:
In my continuing inability to completely understand regular expressions I have a new one for you. I'd like to capture a string "A" unless it is anywhere in between string "B" and string "C". Therefore some matches are: XYZAHIJ ABC
6
56817
by: Mark Findlay | last post by:
I am trying to figure out how to set up my reg exp search so that the search will only match on the exact word. Here is the current problem code: Word1 = "RealPlayer.exe" Word2 = "Player.exe" RegExp re = Word2; if (re.Find(Word1))
19
2152
by: Tom Deco | last post by:
Hi, I'm trying to use a regular expression to match a string containing a # (basically i'm looking for #include ...) I don't seem to manage to write a regular expression that matches this. My (probably to naive) approach is: p = re.compile(r'\b#include\b) I also tried p = re.compile(r'\b\#include\b) in a futile attempt to use a backslash as escape character before the #
2
4748
by: brad | last post by:
Hello all, I'm new to javascript--not too new to a few other programming languages--and I need your help deciphering the Regexp in the following string. Regular expresions are hard enough in Python, and since I am new to javascript they are even harder. Well here's the string, thanks for any and all help I receive. document.URL.match(/^(.+?)(?:\?(?:(.*?)@)?(.+))?$/)
4
11233
by: jmdaviault | last post by:
I want to do the equivalent of SELECT id from TABLE WHERE text='text' only fast solution I found is: SELECT id,text from TABLE WHERE MATCH(text) AGAINST('value' IN BOOLEAN MODE) HAVING text='value'
1
1681
by: Lance Hoffmeyer | last post by:
Hey all, I have a search: VAR = re.search("PROVEN.*?\n+\d(.*?)\n.*?" , pcpT9, re.S ).group(1) #.split() that does not match (sometimes it will and sometimes it will not match) Traceback (most recent call last): File "P:\Burke\TRACKERS\Ortho-McNeil\Automation\Wave3\test.py", line 53, in ?
3
2618
by: Hrvoje Niksic | last post by:
I often have the need to match multiple regexes against a single string, typically a line of input, like this: if (matchobj = re1.match(line)): ... re1 matched; do something with matchobj ... elif (matchobj = re2.match(line)): ... re2 matched; do something with matchobj ... elif (matchobj = re3.match(line)): .....
19
3170
by: konrad Krupa | last post by:
I'm not expert in Pattern Matching and it would take me a while to come up with the syntax for what I'm trying to do. I hope there are some experts that can help me. I'm trying to match /d/d/d/s/d/d in any text. There could be spaces in front or after the pattern (the nnn nn could be without spaces also) but it shouldn't pick it up in case like this 1234 56768
1
1002
by: Peng Yu | last post by:
Hi, '\b' only match the boundary between alphanumerical char and nonalphanumerical char. I'm wonder if there is a generic metacharacter to match the boundary between any non intersected char set defined in regex and its complement. For example, I have a few non intersected char sets , and , and lets call the new metacharactor \m.
0
8600
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
9155
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...
0
9018
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8858
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7711
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...
0
4360
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3038
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2322
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1997
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.