RE: Python regex pattern from array of hex chars
-Original Message- From: Python-list On Behalf Of MRAB Sent: Friday, April 13, 2018 12:05 PM To: python-list@python.org Subject: Re: Python regex pattern from array of hex chars > Use re.escape: > > regex = re.compile('[^{}]+'.format(re.escape(''.join(c for c in > character_class Brilliant, thanks! -- https://mail.python.org/mailman/listinfo/python-list
Re: Python regex pattern from array of hex chars
On 2018-04-13 18:28, Joseph L. Casale wrote: I have an array of hex chars which designate required characters. and one happens to be \x5C or "\". What foo is required to build the pattern to exclude all but: regex = re.compile('[^{}]+'.format(''.join(c for c in character_class))) I would use that in a re.sub to collapse and replace all but those in the character_class. Obviously the escape issues break the \x5C character. Use re.escape: regex = re.compile('[^{}]+'.format(re.escape(''.join(c for c in character_class -- https://mail.python.org/mailman/listinfo/python-list