Split string with % as TEXT format and preserve % symbol - google-sheets

I'm trying to split a string by delimiter "a". I will be performing regexmatches on the first column. However, most values are being split into NUMBER formats that cannot be override. Especially I would like to preserve the percent entries as text format.
Current, only b90 is recognized as ISTEXT
b90a100a100 -> b90 100 100
90%a100a100 -> .9 100 100
100a100a100 -> 100 100 100
Target, I would like at least column A (b90, 90%, 100) to be text.
b90a100a100 -> b90 100 100
90%a100a100 -> 90% 100 100
100a100a100 -> 100 100 100

A simple method is using TRIM or SUBSTITUTE, whose output is always ISTEXT:
=ARRAYFORMULA(TRIM(SPLIT(A1:A3,"a")))
Or
=ARRAYFORMULA(SPLIT(A7:A9,"a")&"")

Please try:
=REGEXEXTRACT(A1,REPT("(.*)?a",LEN(A1)-len(SUBSTITUTE(A1,"a","")))&"(.*)?")
LEN(A1)-len(SUBSTITUTE(A1,"a","")) is a number of letters "a"
REPT("(.*)?a", times) & "(.*)?" makes regex like "(.*)?a(.*)?a(.*)?"
REGEXEXTRACT splits string into strings.

Format the three cells concerned as Plain text and apply Data > Split text into columns... with a as Custom Separator.

Related

How can I split a string and sum all numbers from that string?

I'm making a list for buying groceries in Google Sheets and have the following value in cell B4.
0.95 - Lemon Juice
2.49 - Pringle Chips
1.29 - Baby Carrots
9.50 - Chicken Kebab
What I'm trying to do is split using the dash character and combine the costs (0.95+2.49+1.29+9.50).
I've tried to use Index(SPLIT(B22,"-"), 7) and SPLIT(B22,"-") but I don't know how to use only numbers from the split string.
Does someone know how to do this? Here's a sample sheet.
Answer
The following formula should produce the result you desire:
=SUM(ARRAYFORMULA(VALUE(REGEXEXTRACT(SPLIT(B4,CHAR(10)),"(.*)-"))))
Explanation
The first thing to do is to split the entry in B4 into its component parts. This is done by using the =SPLIT function, which takes the text in B4 and returns a separate result every time it encounters a specific delimiter. In this case, that is =CHAR(10), the newline character.
Next, all non-number information needs to be removed. This is relatively easy in your sample data because the numbers always appear to the left of a dash. =REGEXEXTRACT uses a regular expression to only return the text to the left of the dash.
Before the numbers can be added together, however, they must be converted to be in a number format. The =VALUE function is used to convert each result from a text string containing a number to an actual number.
All of this is wrapped in an =ARRAYFORMULA so that =VALUE and =REGEXEXTRACT parse each returned value from =SPLIT, rather than just the first.
Finally, all results are added together using =SUM.
Functions used:
=CHAR
=SPLIT
=REGEXEXTRACT
=VALUE
=ARRAYFORMULA
=SUM
Firstly you can add , symbols start and ends of numbers with below code:
REGEXREPLACE(B4,"([0-9\.]+)",",$1,")
Then split it based of , sign.
SPLIT(A8, ",")
Try below formula (see your sheet)-
=SUM(ArrayFormula(--REGEXEXTRACT(SPLIT(B4,CHAR(10)),"-*\d*\.?\d+")))

Extracting numbers with REGEXEXTRACT that might have a comma or dot

I have a list of numbers in a few formats that may or may not include a dot and a comma. The numbers are locked in a string. For example:
hello 1,000 goodbye
hola 2,000.12 ciao
Hallo 3000.00 Auf Wiedersehen
How can I extract the numbers?
I don't care if the comma is added but the dot is obviously important.
I need the regular_expression to be used in REGEXEXTRACT (and the rest of the REGEX formulas.
The output should be:
1000
2000.12
3000.00
Supposing that your raw data is in A2:A, use this in B2 (or the second cell) of an otherwise empty column:
=ArrayFormula(IF(A2:A="",,IFERROR(VALUE(REGEXEXTRACT(A2:A,"\d[\d,\.]*\d")))))
The REGEX portion reads, in plain English, "Extract any portion that starts with a digit followed by any number of digits, commas or periods (or none of these) and ends with a digit."
You will likely want to apply Format > Number > Currency to the results column.

google-sheet : extract sub text from text

I have cells and each contains these text :
my_col_10x50_new
my_col_20x40_new
my_col_30x30_new
my_col_15x50_new
I want correspondingly to extract just the number parts and make a "xx and xx" format like these :
10 and 50
20 and 40
30 and 30
15 and 50
Anyone can help ?
You can try a combination of substitute and regex extract:
=SUBSTITUTE(REGEXEXTRACT(A1, "[0-9]+x[0-9]+"),"x", " and ")
The REGEXEXTRACT extracts the numbers along with the x in the middle and substitute replaces the x with the 'and' in the middle keeping the numbers as is.
use:
=INDEX(IFNA(REGEXREPLACE(REGEXEXTRACT(A1:A, "\d+x\d+"), "x", " and ")))
Another option:
=INDEX(SPLIT(SUBSTITUTE(A1:A,"x"," and "),"_"),0,3)

How to generate a sequence code string in Rails

I have a model which has a column named code, which is a combination of the model's name column and its ID with leading zeros.
name = 'Rocky'
id = 16
I have an after_create callback which runs and generates the code:
update(code: "#{self.name[0..2].upcase}%.4d" % self.id)
The generated code will be:
"ROC0016"
The code is working.
I found (%.4d" % self.id) from another project, but I don't know how it works.
How does it determine the number of zeros to be preceded based on the passed integer.
You’re using a "format specifier". There are many specifiers, but the one you’re using, "%d", is the decimal specifier:
% starts it. 4 means it should always use at least four numbers, so if the number is only two digits, it gets padded with 0s to fill in the rest of the numbers. The second % means replace 4d with whatever comes after it. So in your case, 4d is getting replaced with "0016".
sprintf has more information about format specifiers.
You can read more about String#% in the documentation also.
After the percentage sign ("%") is a decimal (".") and a number. That number is the number of total digits in the result. If the result is less than this value, additional zeros will be added.
Thus, in this first example, the result is "34" but length was set to "4". The result will have two leading zeros to fill it into four digits.
"This is test string %.4d" % 34
result => "This is test string 0034"
"I want more zeroes in my code %.7d" % 34
result => "I want more zeroes in my code 0000034"

How to concatenate NSNumbers and keep the locale format?

I'm building a calculator like. Every time I press one of the number buttons of the calculator, I would like to have the label displaying the input, to concatenate the new input to the display.
Let's say I press 1 then 2 then 5 and the label displays 125. So far I was converting each number into a string and was appending them one after one. But I would like to make it "localized". So that if the locale is US, for 1,256.43 it display 1,256.43. If the locale is FR, it displays 1 256,43.
For that I was doing the same than before but to convert the number to string I was using a NSNumberFormatter. The problem is when I get the string 1 256 and I convert it into double (to then use the formatter) with NSString(string: "1 256").doubleValue I get 1 and not 1 256
What do I do wrong?

Resources