Google Sheets RegexpReplace with computable replacers - regex

I'm trying to replace a pattern with some string computed with other GSheets functions. For example, I want to make all the int numbers in the string ten times larger: "I want to multiply 2 numbers in this string by 10" should turn into "I want to multiply 20 numbers in this string by 100".
Assuming for short, that my string is in A1 cell, I've tried a construction
REGEXREPLACE(A1, "([0-9]+)", TEXT(10*VALUE("$1"),"###"))
But it seems REGEXREPLACE firstly computes the arguments and only after that yields regular expression rules. So it converts 3rd argument
TEXT(10*VALUE("$1"),"###") => TEXT(10*1,"###") => "10"
and then just replaces all integers in the string with 10.
It turns out, I need to substitute the group $1 BEFORE implementing outer functions in the 3rd argument. Is there any way to do such a thing?

Maybe there's another way. See if this works
=join(" ", ArrayFormula(if(isnumber(split(A1, " ")), split(A1, " ")*10, split(A1, " "))))

try:
=ARRAYFORMULA(JOIN(" ", IFERROR(SPLIT(A1, " ")*10, SPLIT(A1, " "))))
or:
=ARRAYFORMULA(JOIN(" ", IF(ISNUMBER(SPLIT(A1, " ")), SPLIT(A1, " ")*10, SPLIT(A1, " "))))

Related

Return true/false on [strings in a cell] being found in [strings in another cell]?

Cell A1 contains multiple strings, eg "CAT DOG RAT GNU";
Cell B1 contains multiple strings, eg "RAT CAT";
How can I run a test (using formula in C1) to find if all the strings in B1 are present in cell A1?
Returning true/false would be good
Strings not necessarily in the same order, as example above
The number of items can vary
Multiple instances not a problem, so long as they're there
But returns true only if all items in cell B1 are present in cell A1.
So far I've tried transposed-split arrays with vlookups and matches, counts, etc, but nothing working for me. (And maybe regex won't do it as can't loop for each string?)
you can try:
=ARRAYFORMULA(IF(PRODUCT(N(NOT(ISNA(REGEXEXTRACT(SPLIT(B1, " "),
SUBSTITUTE(A1, " ", "|"))))))=1, TRUE))
for more precision you can do:
=ARRAYFORMULA(IF(PRODUCT(N(NOT(ISNA(REGEXEXTRACT(SPLIT(B1, " "),
"^"&SUBSTITUTE(A1, " ", "$|^")&"$")))))=1, TRUE))
then for case insensivity:
=ARRAYFORMULA(IF(PRODUCT(N(NOT(ISNA(REGEXEXTRACT(SPLIT(LOWER(B1), " "),
"^"&SUBSTITUTE(LOWER(A1), " ", "$|^")&"$")))))=1, TRUE))
and true ArrayFormula would be:
=ARRAYFORMULA(IF((A1:A<>"")*(B1:B<>""), IF(REGEXMATCH(TRANSPOSE(QUERY(TRANSPOSE(IFERROR(
REGEXMATCH(IF(SPLIT(B1:B, " ")<>"", SPLIT(LOWER(B1:B), " "), 0),
"^"&SUBSTITUTE(LOWER(A1:A), " ", "$|^")&"$"))),,999^99)), "FALSE"), FALSE, TRUE), ))

proper IF statement issue causing parse error

I am searching for a formula to combine three things. To capture the value of Cell A into cell B. If it has has a number then it should capture that number. If the value of cell A1 is - then it should change to blank in cell B1 and if it has a blank cell in A1 Then it should be blank in B1.
I have tried the formula in google sheet with IF, And OR combining but it is giving an error.
=if(OR(A1="-"," "),(A1=" ", " "))
=if(And(A1="-"," "),(A1=" ", " "))
=IF(A1="-"," ",IF(A1="","",IF(A1=" "," ")))
The expected results are giving an error or it gives the answer as false.
You should take a look at IF documentation to understand it better, but the main thing here is to remember that IF is IF(logical_expression, value_if_true, value_if_false)
The best option here would be to go with player0 solution, because it covers up almost every case: =IF(ISNUMBER(A1), A1, )
But just to help you out on understanding what was wrong with your formulas, even though those solutions don't check if A1 is a number:
=if(OR(A1="-"," "),(A1=" ", " "))
The correct way would be: =if( OR(A1="-",A1=" "), " ", A1)
=if(And(A1="-"," "),(A1=" ", " "))
This one is actually conceptually wrong, because you want to check if A1 = "-" AND A1 =" ", which is impossible and will never be true, because a cell can't be equal to "-" and " " at the same time.
=IF(A1="-"," ",IF(A1="","",IF(A1=" "," ")))
The correct way would be: =IF(A1="-"," ",IF(A1="","",IF(A1=" "," ",A1)))
that is not how you should use IF function.
If the value of cell A1 is - then it should change to blank in cell B1 and if it has blank cell in A1 Then it should be blank in B1
which could be solved like this in B1 cell:
=IF(ISNUMBER(A1), A1, )
and arrayformula of that would be:
=ARRAYFORMULA(IF(ISNUMBER(A1:A*1), A1:A, ))

How to convert a list of string to a string in racket?(leaving the spaces intact)

How do I convert a list of strings into a string in DrRacket? For example, if I have
'("44" "444") convert it into "44 444"?
I tried string-join, but it takes a delimiter and if I put one it replaces the space with the delimiter and if I use "" for the delimiter it simply gets rid of it.
In fact string-join is the right procedure for using in this case, simply use " " (a single space) as delimiter:
(string-join '("44" "444") " ")
=> "44 444"
Just to clarify: in a list the spaces between elements are not considered part of the list, they're there to separate the elements. For example, all these lists are equal and evaluate to the same value:
'("44""444")
'("44" "444")
'("44" "444")
If for some reason you want to consider the spaces as part of the list then you have to explicitly add them as elements in the list:
(define lst '("a" " " "b" " " "c" " " "d"))
(string-join lst "")
=> "a b c d"

Is this regular expression?

This is how to split string in Unityscript from Unity Wiki. However, I don't recognize " "[0]. Is this regular expression? If so, any reference to it? I'm familiar with regular expressions generally and used them a lot, but this syntax is little confusing.
var qualifiedName = "System.Integer myInt";
var name = qualifiedName.Split(" "[0]);
Wiki Reference
On any string, wether it is a variable or a literal (" "), you can use an indexer to get the char at the nth position.
Your codesample is a very weird way of literally defining a char with a space, and could be simplified by using this:
' '
note the single quotes instead of double quotes
As many have already mentioned, " "[0] is the first character of the " " string (which is a System.String, or an array of System.Chars. The problem with UnityScript is that ' ' is interpreted as a String too, so the only way to provide a Char is by slicing.
" "[0] is the first character of the string " ".
typeof " "[0]; // "string"
Your example is strange, because " "[0] and " " are strictly equal.
" "[0] === " "; // true
Reading reference:
Mono Types When a Mono function requires a char as an input, you can
obtain one by simply indexing a string. E.g. if you wanted to pass the
lowercase a as a char, you'd write: "a"[0]
I suppose it's because UnityScript is implemented in Boo and String is provided by mono.

How does "gsub" handle spaces?

I have a character string "ab b cde", i.e. "ab[space]b[space]cde". I want to replace "space-b" and "space-c" with blank spaces, so that the output string is "ab[space][space][space][space]de". I can't figure out how to get rid of the second "b" without deleting the first one. I have tried:
gsub("[\\sb,\\sc]", " ", "ab b cde", perl=T)
but this is giving me "a[spaces]de". Any pointers? Thanks.
Edit: Consider a more complicated problem: I want to convert the string "akui i ii" i.e. "akui[space]i[space]ii" to "akui[spaces|" by removing the "space-i" and "space-ii".
[\sb,\sc] means "one character among space, b, ,, space, c".
You probably want something like (\sb|\sc), which means "space followed by b, or space followed by c"
or \s[bc] which means "space followed by b or c".
s <- "ab b cde"
gsub( "(\\sb|\\sc)", " ", s, perl=TRUE )
gsub( "\\s[bc]", " ", s, perl=TRUE )
gsub( "[[:space:]][bc]", " ", s, perl=TRUE ) # No backslashes
To remove multiple instances of a letter (as in the second example) include a + after the letter to be removed.
s2 <- "akui i ii"
gsub("\\si+", " ", s2)
There is a simple solution to this.
gsub("\\s[bc]", " ", "ab b cde", perl=T)
This will give you what you want.
You can use lookbehind matching like this:
gsub("(?<=\\s)i+", " ", "akui i ii", perl=T)
Edit:
lookbehind is still the way to go, demonstrated with an other example from your original post. Hope this helps.
x<-"ab b cde"
gsub(" b| c", " ",x)
Note the double spaces in the 2nd argument.