escaping

Escaping special characters in Perl regex

﹥>﹥吖頭↗ 提交于 2019-12-30 08:52:49
问题 I'm trying to match a regular expression in Perl. My code looks like the following: my $source = "Hello_[version]; Goodbye_[version]"; my $pattern = "Hello_[version]"; if ($source =~ m/$pattern/) { print "Match found!" } The problem arises in that brackets indicate a character class (or so I read) when Perl tries to match the regex, and the match ends up failing. I know that I can escape the brackets with \[ or \] , but that would require another block of code to go through the string and

Escaping special characters in Perl regex

故事扮演 提交于 2019-12-30 08:51:08
问题 I'm trying to match a regular expression in Perl. My code looks like the following: my $source = "Hello_[version]; Goodbye_[version]"; my $pattern = "Hello_[version]"; if ($source =~ m/$pattern/) { print "Match found!" } The problem arises in that brackets indicate a character class (or so I read) when Perl tries to match the regex, and the match ends up failing. I know that I can escape the brackets with \[ or \] , but that would require another block of code to go through the string and

Replace a character with backslash bug - Python

冷暖自知 提交于 2019-12-30 08:32:14
问题 This feels like a bug to me. I am unable to replace a character in a string with a single backslash: >>>st = "a&b" >>>st.replace('&','\\') 'a\\b' I know that '\' isn't a legitimate string because the \ escapes the last ' . However, I don't want the result to be 'a\\b' ; I want it to be 'a\b' . How is this possible? 回答1: You are looking at the string representation , which is itself a valid Python string literal. The \\ is itself just one slash, but displayed as an escaped character to make

Replace a character with backslash bug - Python

大兔子大兔子 提交于 2019-12-30 08:32:09
问题 This feels like a bug to me. I am unable to replace a character in a string with a single backslash: >>>st = "a&b" >>>st.replace('&','\\') 'a\\b' I know that '\' isn't a legitimate string because the \ escapes the last ' . However, I don't want the result to be 'a\\b' ; I want it to be 'a\b' . How is this possible? 回答1: You are looking at the string representation , which is itself a valid Python string literal. The \\ is itself just one slash, but displayed as an escaped character to make

escape a string for shell commands in Python [duplicate]

十年热恋 提交于 2019-12-30 06:57:27
问题 This question already has answers here : How to escape os.system() calls? (10 answers) Closed 4 years ago . I'm interested to escape a string in Python3.x, such as: SOME_MACRO(a, b) into... SOME_MACRO\(a,\ b\) ... so that it can be passed to a program (not gcc in this case) as a define, eg, some_program -DSOME_MACRO\(a,\ b\)="some expression" I would expect shlex would have this functionality but I didn't find how to do this and checked many similar questions already. I don't mind to write

escape a string for shell commands in Python [duplicate]

落花浮王杯 提交于 2019-12-30 06:56:10
问题 This question already has answers here : How to escape os.system() calls? (10 answers) Closed 4 years ago . I'm interested to escape a string in Python3.x, such as: SOME_MACRO(a, b) into... SOME_MACRO\(a,\ b\) ... so that it can be passed to a program (not gcc in this case) as a define, eg, some_program -DSOME_MACRO\(a,\ b\)="some expression" I would expect shlex would have this functionality but I didn't find how to do this and checked many similar questions already. I don't mind to write

Swift UITesting error: Invalid escape sequence in literal. \U201c

◇◆丶佛笑我妖孽 提交于 2019-12-30 06:12:09
问题 I am building an automation suite using Xcode 7 with swift. My app loads with the following Alert View: Allow "Light Alarm" to access your location while you use the app? When I record with UI Testing and click this alert I get the following code: app.alerts["Allow \U201cLight Alarm\U201c to access your location while you use the app?"] Note: The quotes has been replaced with \U201c However, when I try and compile I get the following error: "Invalid escape sequence in literal" Anyone know how

Python version of PHP's stripslashes

风流意气都作罢 提交于 2019-12-30 06:04:28
问题 I wrote a piece of code to convert PHP's striplashes into valid Python [backslash] escapes: cleaned = stringwithslashes cleaned = cleaned.replace('\\n', '\n') cleaned = cleaned.replace('\\r', '\n') cleaned = cleaned.replace('\\', '') How can I condense it? 回答1: Not totally sure this is what you want, but.. cleaned = stringwithslashes.decode('string_escape') 回答2: It sounds like what you want could be reasonably efficiently handled through regular expressions: import re def stripslashes(s): r =

Python version of PHP's stripslashes

◇◆丶佛笑我妖孽 提交于 2019-12-30 06:04:16
问题 I wrote a piece of code to convert PHP's striplashes into valid Python [backslash] escapes: cleaned = stringwithslashes cleaned = cleaned.replace('\\n', '\n') cleaned = cleaned.replace('\\r', '\n') cleaned = cleaned.replace('\\', '') How can I condense it? 回答1: Not totally sure this is what you want, but.. cleaned = stringwithslashes.decode('string_escape') 回答2: It sounds like what you want could be reasonably efficiently handled through regular expressions: import re def stripslashes(s): r =

Prevent django admin from escaping html

浪尽此生 提交于 2019-12-30 02:32:07
问题 I'm trying to display image thumbnails in django admin's list_display and I am doing it like this: from django.utils.safestring import mark_safe class PhotoAdmin(admin.ModelAdmin): fields = ('title', 'image',) list_display = ('title', '_get_thumbnail',) def _get_thumbnail(self, obj): return mark_safe(u'<img src="%s" />' % obj.admin_thumbnail.url) Admin keeps displaying the thumbnail as escaped html, although I marked the string as safe. What am I doing wrong? 回答1: As of Django 1.9, you can