Regular expression
MRAB
python at mrabarnett.plus.com
Fri Nov 5 22:16:00 EDT 2010
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)_\(['"]([^'"]+)['"]\)''')
More information about the Python-list
mailing list