python - Properly escaping quotations within a string -



python - Properly escaping quotations within a string -

i have regex supposedly be-all-end-all detecting html tags. found here:

http://haacked.com/archive/2004/10/25/usingregularexpressionstomatchhtml.aspx/

the original regex below:

</?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)/?>

when add together single quotes around it, becomes:

'</?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)/?>'

but leaves inner part ('.\*?') different entity string want make. ideas how prepare this? escaping inner quotes around .*? doesn't seem help since eol while scanning string error

any ideas?

you can utilize double quotes quote entire regex.

>>> obj = re.compile(r"</?\w+((\s+\w+(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)/?>") >>> obj.pattern '</?\\w+((\\s+\\w+(\\s*=\\s*(?:\\".*?\\"|\'.*?\'|[^\'\\">\\s]+))?)+\\s*|\\s*)/?>'

note have escaped double quotes within regex \"

python regex string

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -