|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
Regular Expression
I created a simple function to check for regular expressions, but it returns with no matches and I can't figure out what is wrong with it. Any help would be great. Thanks
--------------------------------------------------- Code:
import urllib import re def Content(content): required = 'a' p = re.compile(required, re.IGNORECASE) #print content print "\n\nMatch is: \n" gr = p.match(content) print gr print "\n\nGroup is: \n" print gr.groups() s = 'japana' Content(s) I keep getting "None" matches. ![]() |
|
#2
|
|||
|
|||
|
The problem is that regexObj.match() only matches at the start of the string. What you want to use is regexObj.search(), which will find the pattern anywhere in the string.
Also, if you use the matchObj.groups() method then you need to define at least one group in you pattern, e.g. '(a)', otherwise it will always return an empty tuple. If you just want to return the matched string then use matchObj.group(). Dave - The Developers' Coach |
|
#3
|
|||
|
|||
|
Aaaaah, i should have mentioned that. I already tried group, and I had already put the paranthesis around 'a' as '(a)' but I still get "none" as matches
|
|
#4
|
|||
|
|||
|
Figured out the problem when someone told me, that it was looking only for those letters, and not within the words.
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Regular Expression |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|