|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now! |
|
#1
|
|||
|
|||
|
regular expression: how to fail on double characters?
say i have 'hello'
and i want to it to fail when it matches double l, (2 or more actually). any ideas? |
|
#2
|
|||
|
|||
|
import re
str1 = "this is crazy" str2 = "thiis is crazy" if re.search('i{2,}',str1) is not None: print "first" if re.search('i{2,}',str2) is not None: print "second" the '{a,b}' specifies it is looking for between a and b occurences of it. If you leave out the b, it looks for minimum of a occurneces. Similarly with leaving out the a |
|
#3
|
||||
|
||||
|
Lazy's regex will work fine, but it requires that you know what letter will occure twice (or more) before hand.. which can be very restricting! lucky you can match any letter occuring twice using backreferances with no more effort
.. This small Python shell session should work: >>> import re >>> string = 'hello' >>> re.search('([a-z])\\1', string.lower()) != None True >>> string = 'hi there' >>> re.search('([a-z])\\1', string.lower()) != None False >>> Note: because of the lower() method this is effectivly a case insentative match. Mark. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > regular expression: how to fail on double characters? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|