backreference

How to match a regex with backreference in Go?

最后都变了- 提交于 2019-11-26 14:34:03
问题 I need to match a regex that uses backreferences (e.g. \1) in my Go code. That's not so easy because in Go, the official regexp package uses the RE2 engine, one that have chosen to not support backreferences (and some other lesser-known features) so that there can be a guarantee of linear-time execution, therefore avoiding regex denial-of-service attacks. Enabling backreferences support is not an option with RE2. In my code, there is no risk of malicious exploitation by attackers, and I need

Negating a backreference in Regular Expressions

断了今生、忘了曾经 提交于 2019-11-26 12:25:14
问题 if a string has this predicted format: value = \"hello and good morning\" Where the \" (quotations) might also be \' (single quote), and the closing char (\' or \") will be the same as the opening one. I want to match the string between the quotation marks. \\bvalue\\s*=\\s*([\"\'])([^\\1]*)\\1 (the two \\s are to allow any spaces near the = sign) The first \"captured group\" (inside the first pair of brackets) - should match the opening quotation which should be either \' or \" then - I\'m

JavaScript - string regex backreferences

回眸只為那壹抹淺笑 提交于 2019-11-26 12:23:42
问题 You can backreference like this in JavaScript: var str = \"123 $test 123\"; str = str.replace(/(\\$)([a-z]+)/gi, \"$2\"); This would (quite silly) replace \"$test\" with \"test\". But imagine I\'d like to pass the resulting string of $2 into a function, which returns another value. I tried doing this, but instead of getting the string \"test\", I get \"$2\". Is there a way to achieve this? // Instead of getting \"$2\" passed into somefunc, I want \"test\" // (i.e. the result of the regex) str

preg_replace: add number after backreference

谁说胖子不能爱 提交于 2019-11-26 06:48:41
问题 Situation I want to use preg_replace() to add a digit \'8\' after each of [aeiou] . Example from abcdefghij to a8bcde8fghi8j Question How should I write the replacement string? // input string $in = \'abcdefghij\'; // this obviously won\'t work ----------↓ $out = preg_replace( \'/([aeiou])/\', \'\\18\', $in); This is just an example , so suggesting str_replace() is not a valid answer. I want to know how to have number after backreference in the replacement string. 回答1: The solution is to wrap