backreference

Use the backreference in a regex to replace a text dynamically

限于喜欢 提交于 2019-12-13 13:51:32
问题 I want to use the $1 value like an integer. The idea is to replace all numbers from originaltext with the equivalent array values and create a new text. The below desired outcome should be "This is DBValue4, This is DBValue2, This is DBValue7" Also, is there a way to save these backreferences for further use? String[] values = {"DBValue0","DBValue1","DBValue2","DBValue3","DBValue4","DBValue5","DBValue6","DBValue7","DBValue8","DBValue9","DBValue10"}; String originaltext = "This is 4, This is 2

Using backreferences for backreferencesing to a pattern

一个人想着一个人 提交于 2019-12-13 08:07:59
问题 Consider (\w[0-9]).*\1 RegEx, it matches to d1akdhfafd1 , R2ddsfasfasfdsfdR2 , etc. . Is is possible to write a RegEx that that match to following too: D1dfsadfadsfE3 , z6adfdasfdfr2 , e3654654e0 ,....? \w[0-9] is just an example, please consider general form (::A_Complex_Pattern::).*\1 回答1: PHP's regex engine (PCRE) supports recursion. A few others do, but you generally shouldn't count on it. However, if the engine does, then you can insert patterns used elsewhere like this: (\w\d).*(?1)

regex, problem with backreference in pattern with preg_match_all

折月煮酒 提交于 2019-12-12 19:12:23
问题 i wonder what is the problem with the backreference here: preg_match_all('/__\((\'|")([^\1]+)\1/', "__('match this') . 'not this'", $matches); it is expected to match the string between __('') but actually it returns: match this') . 'not this any ideas? 回答1: Make your regex ungreedy: preg_match_all('/__((\'|")([^\1]+)\1/U', "__('match this') . 'not this'", $matches) 回答2: You can't use a backreference inside a character class because a character class matches exactly one character, and a

Negative lookahead with capturing groups

人盡茶涼 提交于 2019-12-12 10:45:31
问题 I'm attempting this challenge: https://regex.alf.nu/4 I want to match all strings that don't contain an ABBA pattern. Match: aesthophysiology amphimictical baruria calomorphic Don't Match anallagmatic bassarisk chorioallantois coccomyces abba Firstly, I have a regex to determine the ABBA pattern. (\w)(\w)\2\1 Next I want to match strings that don't contain that pattern: ^((?!(\w)(\w)\2\1).)*$ However this matches everything. If I simplify this by specifying a literal for the negative

Can't use '\1' backreference to capture-group in a function call in re.sub() repr expression

巧了我就是萌 提交于 2019-12-11 06:00:41
问题 I have a string S = '02143' and a list A = ['a','b','c','d','e'] . I want to replace all those digits in 'S' with their corresponding element in list A . For example, replace 0 with A[0] , 2 with A[2] and so on. Final output should be S = 'acbed' . I tried: S = re.sub(r'([0-9])', A[int(r'\g<1>')], S) However this gives an error ValueError: invalid literal for int() with base 10: '\\g<1>' . I guess it is considering backreference '\g<1>' as a string. How can I solve this especially using re

Insert into SQL server from Regular Expression returned text

谁都会走 提交于 2019-12-11 05:41:52
问题 In SQL Server 2008, I have built a regex match and replace function from below code project site and is working well. http://www.codeproject.com/KB/string/SqlRegEx.aspx?msg=3683405#xx3683405xx. This functions basically looks up column text, finds match and replaces with the replaced text. I have used back reference here. e.g. if Column1 had 'the first article #345 is refered by 9999 and place in 001', it will return 345#9999#001 The select statement Select column1, dbo.ufn_RegExReplace

Matching and replacing function expressions

时光怂恿深爱的人放手 提交于 2019-12-11 01:39:41
问题 I need to do some very light parsing of C# (actually transpiled Razor code) to replace a list of function calls with textual replacements. If given a set containing {"Foo.myFunc" : "\"def\"" } it should replace this code: var res = "abc" + Foo.myFunc(foo, Bar.otherFunc( Baz.funk())); with this: var res = "abc" + "def" I don't care about the nested expressions. This seems fairly trivial and I think I should be able to avoid building an entire C# parser using something like this for every

How to replace multiple matches / groups with regexes?

女生的网名这么多〃 提交于 2019-12-10 21:24:37
问题 Normally we would write the following to replace one match: namesRegex = re.compile(r'(is)|(life)', re.I) replaced = namesRegex.sub(r"butter", "There is no life in the void.") print(replaced) output: There butter no butter in the void. What i want is to replace, probably using back references, each group with a specific text. Namely i want to replace the first group (is) with "are" and the second group (life) with "butterflies". Maybe something like that. But the following is not working code

Regexp: How to match a string that doesn't have any character repeated 3 times?

吃可爱长大的小学妹 提交于 2019-12-10 21:05:25
问题 I'm trying to make a single pattern that will validate an input string. The validation rule does not allow any character to be repeated more that 3 times in a row. For example: Aabcddee - is valid. Aabcddde - is not valid, because of 3 d chracters. The goal is to provide a RegExp pattern that could match one of above examples, but not both. I know I could use back-references such as ([a-z])\1{1,2} but this matches only sequential characters. My problem is that I cannot figure out how to make

Python regex subsitution: separate backreference from digit

浪子不回头ぞ 提交于 2019-12-09 02:20:05
问题 In a regex replacement pattern, a backreference looks like \1 . If you want to include a digit after that backreference, this will fail because the digit is considered to be part of the backreference number: # replace all twin digits by zeroes, but retain white space in between re.sub(r"\d(\s*)\d", r"0\10", "0 1") >>> sre_constants.error: invalid group reference Substitution pattern r"0\1 0" would work fine but in the failing example back-reference \1 is interpreted as \10 . How can the digit