backreference

Named backreferences with preg_replace

旧时模样 提交于 2019-11-30 12:37:16
Pretty straightforward; I can't seem to find anything definitive regarding PHP's preg_replace() supporting named backreferences: // should match, replace, and output: user/profile/foo $string = 'user/foo'; echo preg_replace('#^user/(?P<id>[^/]+)$#Di', 'user/profile/(?P=id)', $string); This is a trivial example, but I'm wondering if this syntax, (?P=name) is simply not supported. Syntactical issue, or non-existent functionality? They exist: http://www.php.net/manual/en/regexp.reference.back-references.php With preg_replace_callback: function my_replace($matches) { return '/user/profile/' .

General approach for (equivalent of) “backreferences within character class”?

只谈情不闲聊 提交于 2019-11-30 01:29:40
问题 In Perl regexes, expressions like \1 , \2 , etc. are usually interpreted as "backreferences" to previously captured groups, but not so when the \1 , \2 , etc. appear within a character class. In the latter case, the \ is treated as an escape character (and therefore \1 is just 1 , etc.). Therefore, if (for example) one wanted to match a string (of length greater than 1) whose first character matches its last character, but does not appear anywhere else in the string, the following regex will

Can't get Python regex backreferences to work

强颜欢笑 提交于 2019-11-29 15:47:16
I want to match the docstrings of a Python file. Eg. r""" Hello this is Foo """ Using only """ should be enough for the start. >>> data = 'r""" Hello this is Foo\n """' >>> def display(m): ... if not m: ... return None ... else: ... return '<Match: %r, groups=%r>' % (m.group(), m.groups()) ... >>> import re >>> print display(re.match('r?"""(.*?)"""', data, re.S)) <Match: 'r""" Hello this is Foo\n """', groups=(' Hello this is Foo\n ',)> >>> print display(re.match('r?(""")(.*?)\1', data, re.S)) None Can someone please explain to me why the first expression matches and the other does not? You

Backreferences Syntax in Replacement Strings (Why Dollar Sign?)

最后都变了- 提交于 2019-11-28 22:18:55
In Java, and it seems in a few other languages, backreferences in the pattern are preceded by a backslash (e.g. \1 , \2 , \3 , etc), but in a replacement string they preceded by a dollar sign (e.g. $1 , $2 , $3 , and also $0 ). Here's a snippet to illustrate: System.out.println( "left-right".replaceAll("(.*)-(.*)", "\\2-\\1") // WRONG!!! ); // prints "2-1" System.out.println( "left-right".replaceAll("(.*)-(.*)", "$2-$1") // CORRECT! ); // prints "right-left" System.out.println( "You want million dollar?!?".replaceAll("(\\w*) dollar", "US\\$ $1") ); // prints "You want US$ million?!?" System

How do backreferences in regexes make backtracking required?

喜夏-厌秋 提交于 2019-11-28 18:48:13
I read http://swtch.com/~rsc/regexp/regexp1.html and in it the author says that in order to have backreferences in regexs, one needs backtracking when matching, and that makes the worst-case complexity exponential. But I don't see exactly why backreferences introduce the need for backtracking. Can someone explain why, and perhaps provide an example (regex and input)? To get directly at your question, you should make a short study of the Chomsky Hierarchy . This is an old and beautiful way of organizing formal languages in sets of increasing complexity. The lowest rung of the hierarchy is the

Can't get Python regex backreferences to work

て烟熏妆下的殇ゞ 提交于 2019-11-28 09:26:07
问题 I want to match the docstrings of a Python file. Eg. r""" Hello this is Foo """ Using only """ should be enough for the start. >>> data = 'r""" Hello this is Foo\n """' >>> def display(m): ... if not m: ... return None ... else: ... return '<Match: %r, groups=%r>' % (m.group(), m.groups()) ... >>> import re >>> print display(re.match('r?"""(.*?)"""', data, re.S)) <Match: 'r""" Hello this is Foo\n """', groups=(' Hello this is Foo\n ',)> >>> print display(re.match('r?(""")(.*?)\1', data, re.S)

Applying a function to a backreference within gsub in R

佐手、 提交于 2019-11-28 05:32:49
问题 I'm new to R and am stuck with backreferencing that doesn't seem to work. In: gsub("\\((\\d+)\\)", f("\\1"), string) It correctly grabs the number in between parentheses but doesn't apply the (correctly defined, working otherwise) function f to replace the number --> it's actually the string "\1" that passes through to f. Am I missing something or is it just that R does not handle this? If so, any idea how I could do something similar, i.e. applying a function "on the fly" to the (actually

%N backreference inside RewriteCond

萝らか妹 提交于 2019-11-28 05:03:34
I'm working on a virtual domain system. I have a wildcard DNS set up as *.loc , and I'm trying to work on my .htaccess file. The following code works: RewriteEngine On RewriteCond %{HTTP_HOST} ^(www.)?example\.loc$ [NC] RewriteCond %{REQUEST_URI} !^/example/ RewriteRule (.*) /example/$1 [L,QSA] But, I want this to work with anything I put in. However, I need the %{REQUEST_URI} checked against the text found as the domain. I tried using this code: RewriteEngine On RewriteCond %{HTTP_HOST} ^(www.)?([a-zA-Z0-9-]*.)?([a-zA-Z0-9-]+)\.loc$ [NC] RewriteCond %{REQUEST_URI} !^/%3/ RewriteRule (.*) /%3/

python re.sub - alternative replacement patterns

最后都变了- 提交于 2019-11-28 02:12:45
I want to provide alternative replacement patterns to re.sub. Let's say i've got two search patterns as alternatives, like this: re.sub(r"[A-Z]+|[a-z]+", replacementpattern, string) and instead of providing one replacement pattern I would like to somehow catch which search pattern alternative was matched and provide alternative replacement patterns. Is this possible? Thanks. PS. code specifics here are irrelevant, it's a general question. You can pass a function to re.sub() . In the function you can return the value needed based on the captured group. A simple code for illustration: >>> def

Notepad++ Regex Backreference syntax in Search/Replace - \\1 or $1

眉间皱痕 提交于 2019-11-27 21:50:50
I have tried to use the Notepad++ Search/Replace with a Regular Expression to replace specific words with shorter versions of those words. I used the following regex to match every word that ends with er (but not er as a word) - and replace the matching words with the same words minus the ending r , using a backreference: Find what: ([a-zA-z]+e)r Replace with: $1 But it doesn't replace the matching words, even though it finds them. However, if I change the backreference syntax to this: Replace with: \1 Everything works fine. Why doesn't the $1 backreference work? What is the difference between