Replace a Text with a Variable

Find and replace with variable text

You could use 2 capturing groups and in the replacement referer to those capturing groups.

\bSession::flash\(\s*'([^']+)',\s*('[^']+')\s*\);

In the replacement use:

flash($2)->$1;

Explanation

  • \bSession::flash\(\s* Match a wordboundary to prevent Session being part of a longer word, then match Session::flash( followed by 0+ times a whitespace char
  • '([^']+)' Match ', then capture in group 1 matching not a ' using a negated character class, then match ' again
  • ,\s* Match a comma followed by 0+ times a whitespace char
  • ('[^']+') Capture in group 2 matching ', then not ' and again '
  • \s*\); Match 0+ times a whitespace char followed by );

Regex demo

Result:

flash('Only users with permission may view the directory user.')->error;
flash('System user ID does not exist.')->error;

SH - Replace words in text value by variables

If TEST must be a literal string, you can use parameter substitution. These are bash docs, but it also works in sh:

${var/Pattern/Replacement}:
First match of Pattern, within var replaced with Replacement.
If Replacement is omitted, then the first match of Pattern is replaced by nothing, that is, deleted.

${var//Pattern/Replacement}: Global replacement. All matches of Pattern, within var replaced with Replacement.

For your given example, it would be ${TEST/\$VERSION/$VERSION}:

$ VERSION=1.0.0
$ TEST='This is a test, VERSION: $VERSION'
$ echo "${TEST/\$VERSION/$VERSION}"
This is a test, VERSION: 1.0.0

The first dollar sign is escaped so the Pattern is \$VERSION which becomes the literal string "$VERSION". Then its Replacement is $VERSION which gets interpolated as "1.0.0".

Replace a text with a variable

You need to use double quotes:

$ sed -i "s/wiki_host/${host_name}/g" /root/bin/sync

Your single quotes prevent the shell variable from being replaced with its contents.

gsubfn | Replace text using variables in Substitution

1) gsubfn There are several problems here:

  • the regular expression in gsubfn (and in gsub) must match the string you want to process but a dot matches only a single character so it can never match This or test which are 4 character strings. Use "\\w+" instead.

  • In list(a = x) the a must be a constant, not a variable. Write out the names explicitly or use setNames instead if they are in variables.

Thus to fix up the code in the question:

library(gsubfn)

trimws(gsubfn("\\w+", list(This = "", text = ""), Text))
## [1] "is an example [] test"

or in terms of the header variables:

L <- setNames(list("", ""), c(topheader, bottomheader))
trimws(gsubfn("\\w+", L, Text))
## [1] "is an example [] test"

Note that this will replace any occurrence of topheader and bottomheader and not just ones at the start and end; however, this seems to be the closest to your code that is likely sufficient.

2) sub Another possibility is this simple sub

sub("^This (.*) text$", "\\1", Text)
[1] "is an example [] test"

or in terms of the header variables:

pat <- sprintf("^%s (.*) %s$", topheader, bottomheader)
sub(pat, "\\1", Text)
## [1] "is an example [] test"

Update: Fixed (1)

How to replace a text pattern with a variable

Presuming you mean to ask how to replace a variable name demarcated in a string.

Perl has interpolation, SAS does not.

You will have to choose a demarcating scheme and then using PRXNEXT to find the token within for replacement with a variable value.

Example:

Variable names are demarcated as #<variable-name>#.

Code:

data want;
array patterns(6) $100 _temporary_
( 'The student name is #name#'
, 'I was #name#''s lab partner'
, '#name# is a swell fella'
, 'This won''t work. Next year #name# will be #age#'
, '#name# is #height# inches tall'
, '#000#, not a #replacable variable#. #name# is.'
);

set sashelp.class;
where sex='M';

* pattern to discover a token, i.e. a demarcated variable name;
id = prxparse('/#([_a-z][_a-z0-9]*)#/i');

do index = 1 to dim(patterns);

length result $200;
result = patterns(index);

do start=1 to length(result);

call prxnext(id, start, -1, result, pos, len);

if pos = 0 then leave;

* extract variable name from token;
varname = prxposn(id,1,result);

* replace token with the formatted value of the variable;
result = transtrn(result, cats('#',varname,'#'), strip(vvaluex(varname)));

end;

output;
end;

keep name result;
run;

How to replace text in message with regex with variable get in sheet in google apps script

You need to change your replace() method to:





var regex1 = 'blablabla {name} blablabla {name} blabla';

console.log(regex1.replace(/name/g, 'Changed'));

Powershell - Get text into variable, use text to replace text in another text file

$a.Replace('",$Word', '"$Word') Single quotes here are going to hand back a string literal. You'll get literally the characters between those quotes.

If you want to express the $Word variable you need another attack. Try: $a.Replace("`",$Word", "`"$Word")

(Note the backtick to escape the double-quote that you want to replace.)

Replacing text in a file using sed with an environment variable

You may try this:

find . -type f -name "*.js" -exec sed -i "s~http://localhost:3010~$SERVER~g" {} +

So using double quotes so that shell can expand $SERVER and also used alternate delimiter ~ because / is part of your variable.

JavaScript: Replace part of the text is stored in a string variable that starts with '$ ?

Does something like this matches your needs ?