Regular expression

John Bond lists at asd-group.com
Sat Nov 6 03:24:13 EDT 2010


On 6/11/2010 2:16 AM, MRAB wrote:
> On 06/11/2010 01:25, Paul Hemans wrote:
>> I need to extract the quoted text from :
>> _("get this")
>>
>> The following works:
>> re.compile( "_\(['\"]([^'\"]+)['\"]\)" )
>> However, I don't want to match if there is A-Z or a-z or 0-9 or _
>> immediately preceding the "_" so I have tried:
>> "[^0-9a-zA-Z]*_\(['\"]([^'\"]+)['\"]\)"
>> "[^\w]{0,1}_\(['\"]([^'\"]+)['\"]\)"
>> "\W*_\(['\"]([^'\"]+)['\"]\)"
>>
>> to match against:
>> skip this text _("get this")
>>
> Use a negative lookbehind:
>
>     re.compile(r'''(?<!\w)_\(['"]([^'"]+)['"]\)''')

Slightly improved version that doesn't allow mixed quotes, eg. __("fred') :

re.compile(r'''(?<!\w)_\((['"])([^'"]+)\1\)''')

The starting quote is now captured as group 1, and has to terminate the 
string too (use of \1).

Cheers, JB








More information about the Python-list mailing list