What is the equivalent of Regex-replace-with-function-evaluation in Java 7?

Your answer is in the Matcher#appendReplacement documentation. Just put your function call in the while loop. [The appendReplacement method] is intended to be used in a loop together with the appendTail and find methods. The following code, for example, writes one dog two dogs in the yard to the standard-output stream: Pattern p = Pattern.compile(“cat”); … Read more

Passing a function to Powershell’s (replace) function

Perhaps you are looking for Regex.Replace Method (String, MatchEvaluator). In PowerShell a script block can be used as MatchEvaluator. Inside this script block $args[0] is the current match. $global_counter = 0 $callback = { $global_counter += 1 “string-$($args[0])-” + $global_counter } $re = [regex]”match” $re.Replace(‘zzz match match xxx’, $callback) Output: zzz string-match-1 string-match-2 xxx

How to search and replace with a counter-based expression in Vim?

It is possible to have a counter using the substitute-with-an-expression feature (see :help sub-replace-\=). Unfortunately, since the \= construct allows only expressions, the :let command cannot be used, and therefore, a variable cannot not be set the usual way. However, there is a simple trick to change the value of a variable in expression if that variable … Read more

How to replace elements in a list using dictionary lookup

If all values are unique then you should reverse the dict first to get an efficient solution: >>> subs = { … “Houston”: “HOU”, … “L.A. Clippers”: “LAC”, … … } >>> rev_subs = { v:k for k,v in subs.iteritems()} >>> [rev_subs.get(item,item) for item in my_lst] [‘L.A. Clippers’, ‘Houston’, ’03/03 06:11 PM’, ‘2.13’, ‘1.80’, ’03/03 … Read more