substitution

How do I substitute overlapping matches with a Perl regex?

六月ゝ 毕业季﹏ 提交于 2019-12-12 12:26:08
问题 I want to find all occurences of "BBB" in a string and substitute them with "D" . For example, I have "ABBBBC" and want to produce "ADBC" and "ABDC" . (First substitute the first BBB , and then substitute the other BBB ). Is there a nice way to do this in Perl? $str = "ABBBBC"; for ( $str =~ m/B(?=BB)/g ) { # I match both the BBBs here, but how to substitute the relevant part? } I want to get this array: ('ADBC', 'ABDC') , which comes from changing either of the BBB s to a D . The string

parse values based on groups in R

五迷三道 提交于 2019-12-12 12:16:38
问题 I have a very large dataset and a sample of that looks something like the one below: | Id | Name | Start_Date | End_Date | |----|---------|------------|------------| | 10 | Mark | 4/2/1999 | 7/5/2018 | | 10 | | 1/1/2000 | 9/24/2018 | | 25 | | 5/3/1968 | 6/3/2000 | | 25 | | 6/6/2009 | 4/23/2010 | | 25 | Anthony | 2/20/2010 | 7/21/2016 | | 25 | | 9/12/2014 | 11/26/2019 | I need to parse the names from Name column based on their Id such that the output table looks like: | Id | Name | Start_Date

substitute sympy function with arbitrary arguments

前提是你 提交于 2019-12-12 11:57:09
问题 This should be an easy task, but I'm having a hard time getting it to work in Sympy. I want to substitute an undefined function with arbitrary arguments with a specific formula for example: from sympy import * var('a b c') f= Function('f') test= f(a+b) lin= test.subs({f(c):2*(c)}) print(lin) I want this to print out 2*(a+b) However, for that I have to use lin= test.subs({f(a+b):2*(a+b)}) Do I have to define f as a class in order to do this substitution? 回答1: When you're doing advanced

Inline regex replacement in perl

不羁岁月 提交于 2019-12-12 09:36:19
问题 Is there a way to replace text with a regex inline, rather than taking the text from a variable and storing it in a variable? I'm a perl beginner. I often find myself writing my $foo = $bar; $foo =~ s/regex/replacement/; doStuff($foo) where I'd really like to write doStuff($bar->replace(s/regex/replacement/)); or the like, rather than using a temporary variable and three lines. Is there a way to do this? Obviously when the regex is sufficiently complicated it makes sense to split it out so it

Python Regex Sub: Using Dictionary with Regex Expressions

為{幸葍}努か 提交于 2019-12-12 05:37:50
问题 I am using a dictionary that contain regular expressions to substitute portions of different strings, as elegantly described in a previous SO question by @roippi. The first 're.sub' expression works perfectly. However, whenever my code actually involves regex expressions (the second 're.sub' expression), the substitutions don't work. I am very confused as to why this is the case. I have tried both using and taking out the 'r' as well as incorporating the lookahead/lookbehind expressions,

Is There a Variant of regex_replace that Supports Inline Code?

扶醉桌前 提交于 2019-12-12 03:08:02
问题 Perl has the e regex modifier which allows Perl code rather than just a string to formulate the replacement: http://perldoc.perl.org/perlretut.html#Search-and-replace Though that example is not the greatest as there are switches to accomplish this. For those of you who understand Perl here's an example that makes more sense: $string = "StackOverflow user: Jonathan Mee"; $string =~ s/:\s*(.*)$/$1 == "Jonathan Mee" ? ": ".$1." is AWESOME!" : ": ".$1." is ???"/e; print $string; #Will print

Substituting array elements from one tab delimited file with hash values from another file using Perl

限于喜欢 提交于 2019-12-11 17:43:39
问题 I would like to substitute each element in an array with their corresponding hash values. To make it more clear: I have two files 1) ref.tab 2) data.tab. The reference file contains data like: A a B b C c D d The data file contains data like: 1 apple red A 2 orange orange B 3 grapes black C 4 kiwi green D What I would like to do now using Perl is: Substitute all instances of values in column 4 of data.tab with the corresponding values from ref.tab. My code is as follows: #!/usr/bin/perl use

regex substitute several special characters with other special characters in Textwrangler

泪湿孤枕 提交于 2019-12-11 15:39:59
问题 The character ̈ (unicode 0x308) cannot be represented in the “Western (ISO Latin 9)” encoding. I need to replace several (3) of this special characters in many txt-files. Ideal would be one single regex command for the TEXTWRANGLER editor application I run on my Mac so I can use in the find&replace function of Textwrangler (similar to BBedit). Here are the 3 special chars: ä into ä ö into ö ü into ü (please note the first letter persists of two chars (e.g. the a and the ̈ unicode 0x308)

sed substitution with bash variables

这一生的挚爱 提交于 2019-12-11 14:27:33
问题 Trying to change values in a text file using sed in a bash script with the line, sed 's/draw($prev_number;n_)/draw($number;n_)/g' file.txt > tmp This will be in a for loop. Not sure why it's not working. Any suggestions? 回答1: Variables inside ' don't get substituted in bash. To get string substitution (or interpolation, if you're familiar with perl) you would need to change it to use double quotes " instead of the single quotes: $ # enclose entire expression in double quotes $ sed "s/draw(

How do I replace vowels in a list with underscore?

我的未来我决定 提交于 2019-12-11 12:14:32
问题 I have a list of characters (basically a word) and I want to substitute the vowels in the word by underscore ['_'] and return a new list. eg: ?-sub([s,e,g,e,d],A). A=[s,_,g,_,d] My attempt built the new list in the reverse order then finally when it exited from the predicate call it dismantle everything and declare that it found the goal without the output string! The algorithm I used is: pick element of a list and check whether it is a vowel or not and append the ['_'] or the element itself