Google Sheets regexextract multiple text strings from a cell - regex

I am trying to extract the hashtag info from a twitter data cell in google sheets.
We can call this Cell A1:
[{"text":"QOTD","indices":[13,18]},{"text":"CSEC4CG","indices":[87,95]},{"text":"myCSEC","indices":[96,103]},{"text":"Connecticut","indices":[104,116]},{"text":"GiveBack","indices":[117,126]},{"text":"COVID19","indices":[127,135]}]
In a perfect situation I would be able to produce this in another cell extracted from A1:
#QOTD #CSEC4CG #myCSEC #Connecticut #Giveback #COVID19
I am lost how to do it using REGEXTRACT. I assume this is the best method, but any that gets the job done is good.
Thank you for any help!

You want to achieve the following conversion using the built-in functions in Google Spreadsheet.
From
[{"text":"QOTD","indices":[13,18]},{"text":"CSEC4CG","indices":[87,95]},{"text":"myCSEC","indices":[96,103]},{"text":"Connecticut","indices":[104,116]},{"text":"GiveBack","indices":[117,126]},{"text":"COVID19","indices":[127,135]}]
To
#QOTD #CSEC4CG #myCSEC #Connecticut #Giveback #COVID19
If my understanding is correct, how about this answer?
Sample formula:
=TRIM(REGEXREPLACE(REGEXREPLACE(A1,"\[|\]|,",""),"{""text"":""(.+?)""""indices"":.+?}"," #$1"))
In this case, it supposes that the input value is put in the cell "A1".
The flow is as follows.
Replace \[|\]|, with "" using REGEXREPLACE.
Replace {""text"":""(.+?)""""indices"":.+?} with " #$1" using REGEXREPLACE.
The top space is removed with TRIM.
Result:
References:
REGEXREPLACE
TRIM

Related

How do I get my regexextract forumula to work in google sheets?

I'm trying to use regexextract on a list of cells in google sheets. I've tested the regular expression and it works for the string I'm trying to extract, but when I use it in google sheets, it seems to be splitting the output into a few different columns.
I tried using this formula:
=REGEXEXTRACT(C107,"[+-]?([0-9]*[.])?[0-9]+X[+-]?([0-9]*[.])?[0-9]+X[+-]?([0-9]*[.])?[0-9]+")
and was expecting this string:
WASHER SS 3.2X6X0.5 A4
to output:
3.2X6X0.5
But instead I was getting:
3. [Blank column] 0.
Split into 3 cells. Hope I've explained this problem clearly enough, and thanks in advance for reading this!
You can use:
=REGEXEXTRACT(C107,".+ (\d.+) ")

REGEX: Transpose each word from a google sheet cell and put them one under the other (rows)

I need to extract each word (phrase) within a cell in google sheets and put each one under the other in a column (row for each one).
I have a regex code that works when testing it, but I cannot do it work in google sheet the same code. Any ideas?
You can just do
=SUBSTITUTE(A1," ",char(10))
or
=transpose(split(A1," "))
You don't really need regex for this. You can use Transpose and split methods of google sheets.
Example:
In sheets in A1 put your text then in B1 copy this =TRANSPOSE(SPLIT(A1," "))
Update: this answer does not meet all requirements as it puts all results in one cell.
The ASCII character numbers need to be used instead of \r\n that you would expect from other tools.
Carriage return has number 13 and line feed has number 10:
=REGEXREPLACE(A1, "\s", CONCAT(CHAR(13), CHAR(10)))

Regexmatch for multiple words in Sheets

I'm trying to write a REGEXMATCH formula for Sheets that will analyze all of the text in a cell and then write a given keyword into another cell.
I've figured out how to do this for a single keyword: for example,
=IF(REGEXMATCH(F3, "czech"),"CZ",IF(REGEXMATCH(F3, "african"),"AF",IF(REGEXMATCH(F3, "mykonos"),"MK")))
What I'm having trouble with though is writing one of these values only if two or more terms are matched in the reference cell.
If I were trying to match one of two words, I realize I could use | as in:
=IF(REGEXMATCH(F3, "czech|coin"),"CZC"
etc
But in this instance I only want to produce CZC if the previous cell contains BOTH czech AND coin.
Can someone help me with this?
try like this:
=IF((REGEXMATCH(F3, "czech"))*(REGEXMATCH(F3, "coin")), "CZC", )
multiplication stands for AND

Avoid duplicate code in Excel IF formula code

I want to avoid duplicate code within excel formulas. Is there a method to repeat a certain code segment?
=IF(A1=1,(A1-B2-C3),(A1-B2-C3)+1)
This would be especially useful when it comes to more complex or longer sections. But: everything must be in ONE formula in ONE cell. Thanks! :-)
EDIT: This is my current code.
=IF(ISNUMBER(SEARCH(".amp",A2)),IFERROR(MID(A2,FIND("#",SUBSTITUTE(A2,"-","#",LEN(A2)-LEN(SUBSTITUTE(A2,"-",""))))+1,SEARCH(".html",A2)-FIND("#",SUBSTITUTE(A2,"-","#",LEN(A2)-LEN(SUBSTITUTE(A2,"-",""))))-5),""),IFERROR(MID(A2,FIND("#",SUBSTITUTE(A2,"-","#",LEN(A2)-LEN(SUBSTITUTE(A2,"-",""))))+1,SEARCH(".html",A2)-FIND("#",SUBSTITUTE(A2,"-","#",LEN(A2)-LEN(SUBSTITUTE(A2,"-",""))))-1),""))
It strips the long ID number out of any URL of a specific CMS. So
FIND("#",SUBSTITUTE(A2,"-","#",LEN(A2)-LEN(SUBSTITUTE(A2,"-","")))
is probably the part which occurs more than once and should be replaced for a code which does not be that duplicate-prone.
EXAMPLE: www.domain.com/path1/path2/this-is-an-article-123-dd-123456789.html --> 1234567890
EXAMPLE: www.domain.com/path1/path2/this-is-an-article-123-dd-1234567890.amp.html ->
1234567890
EXAMPLE: www.domain.com/path1/this-is-an-article-1234567890.html ->
1234567890
In google sheets, you could use REGEXEXTRACT to get what you want:
Formula in B1:
=REGEXEXTRACT(A1,"\d{8,}")
Place the complex common sub-expression in its own cell and refer to that cell.
EDIT#1:
As an alternative, you can use a Named Formula for the sub-expression:
Named Formula
So here is another way of finding the code in Excel:
Here is the formula in Cell B1 which needs to be confirmed by pressing Ctrl+Shift+Enter, then drag it down to apply across board:
{=FILTERXML("<data><a>"&SUBSTITUTE(MID(A1,LARGE(IF(MID(A1,ROW($A$1:INDEX($A:$A,LEN(A1))),1)="-",ROW($A$1:INDEX($A:$A,LEN(A1)))),1)+1,LEN(A1)),".","</a><a>")&"</a></data>","/data/a[1]")}
For the logic behind this formula you may give a read to this article: Extract Words with FILTERXML.
Cheers :)
Ps. it seems that GoogleSheet has out performed Excel in some area already.

Google sheet formula to swap names

I have this:
Image shows that those 2 cells contain input from another sheet.
Now, I want to input formula in those same cells that will rename inputs:
=if(D5="jerry.berry", "Berry Jerry", if(D5="perkins.aaron", "Aaron Perkins"))
Question: How do I insert the above formula next to =Source!H5
Note: I know I can modify those values on the root sheet itself, but I would rather not.
you can use regex like:
=ARRAYFORMULA(PROPER(REGEXREPLACE(Source!H5:H6, "(.+)\.(.+)", "$2 $1")))
I hope this is what you need:
Although I'd use a generic formula to manipulate (any) names in this string format.