473,722 Members | 2,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

re.search making no match == 0

Hey all,

I have a search:

VAR = re.search("PROV EN.*?\n[\sA-Za-z\(\)\/\-]+\d(.*?)\n.*?" , pcpT9, re.S ).group(1) #.split()[1]

that does not match (sometimes it will and sometimes it will not match)

Traceback (most recent call last):
File "P:\Burke\TRACK ERS\Ortho-McNeil\Automati on\Wave3\test.p y", line 53, in ?
VAR = re.search("PROV EN.*?\n[\sA-Za-z\(\)\/\-]+\d(.*?)\n.*?" , pcpT9, re.S ).group(1) #.split()[1]
AttributeError: 'NoneType' object has no attribute 'group'
Is there a way in python to make this "non" match equal a value (say, "0")?

This one line is actually a series of searches

Reasons = ["EFFICACY","EFF ECTIVE","WORKS QUICKLY","PROVE N","SAFETY","LI TTLE","WELL"," BETTER","SAMPLE S"] # Reasons(29)
VAR = [re.search(r"%s. *?\n[\sA-Za-z\(\)\/\-]+\d(.*?)\n.*?" % i, pcpT9, re.S ).group(1) for i in Reasons ] #.split()[1]

and one of the items in the array may or may not be present.

Lance
Feb 15 '07 #1
1 1681
Lance Hoffmeyer wrote:
I have a search:

VAR = re.search("PROV EN.*?\n[\sA-Za-z\(\)\/\-]+\d(.*?)\n.*?" , pcpT9, re.S
).group(1) #.split()[1]

that does not match (sometimes it will and sometimes it will not match)

Traceback (most recent call last):
File "P:\Burke\TRACK ERS\Ortho-McNeil\Automati on\Wave3\test.p y", line 53,
in ?
VAR = re.search("PROV EN.*?\n[\sA-Za-z\(\)\/\-]+\d(.*?)\n.*?" , pcpT9,
re.S ).group(1) #.split()[1]
AttributeError: 'NoneType' object has no attribute 'group'
Is there a way in python to make this "non" match equal a value (say,
"0")?
match = re.search(...)
if match:
value = match.group(1)
else:
value = default

Wrap that into a function and you can use it inside a list comprehension.
This one line is actually a series of searches

Reasons = ["EFFICACY","EFF ECTIVE","WORKS
QUICKLY","PROVE N","SAFETY","LI TTLE","WELL"," BETTER","SAMPLE S"] #
Reasons(29)
VAR = [re.search(r"%s. *?\n[\sA-Za-z\(\)\/\-]+\d(.*?)\n.*?" % i, pcpT9,
re.S ).group(1) for i in Reasons ] #.split()[1]

and one of the items in the array may or may not be present.
How about a slightly different approach (untested):

r = re.compile(r"(% s).*?\n[\sA-Za-z\(\)\/\-]+\d(.*?)\n.*?" %
"|".join(reason s), re.S)
groups = dict(m.groups() for m in r.finditer(pcpT 9))
VAR = [groups.get(reas on, "0") for reason in reasons]

I recommend that you don't create the VAR list and use groups directly.
If you are using Python 2.5 you can replace the builtin dict with a
collections.def aultdict.

Peter
Feb 15 '07 #2

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

Similar topics

6
3331
by: DC | last post by:
Hi, I am programming a search catalogue with 200000 items (and growing). I am currently using the SQL Server 2000 fulltext engine for this task but it does not fit the requirements anymore. The products typically do have a verbose name, "canadian superapples: red tasty juicy macintosh apple from toronto" and the like. If a customer is looking for "canadian apple" this product needs to match, but also if he is looking for "juicy mac".
32
14875
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
7
1941
by: Bonj | last post by:
In making a ternary search tree to identify as fast as possible the type of word passed in to the algorithm, for instance sp_help -> 1 (procedures), select -> 2 (keywords), sysobjects -> 3 (system tables), I'm stuck on deciding what type of comparison happens at each node. Has anyone got any suggestions? Do you think for the first level I could compare just the first characters of the word passed in and the word at the 'current' node, or...
6
6925
by: San | last post by:
Hey, I need to create a form with several text boxes in which users type in key words, press a command button on the form and it opens a matching record. Thanking you in advance.
0
9384
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
9238
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...
1
9157
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
8052
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
5995
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
4502
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...
0
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2602
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2147
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.