Google sheets query function. Use where clause with string that includes apostrophe - google-sheets

How do you use a string with an apostrophe in the where clause of the query function?
I want to be able to filter my query with a string that includes an apostrophe like this.
=QUERY(D:E;"select E where D = 'this string's apostrophe breaks my code'";0)
Anyone know how to make it work?

This can be done by placing two sets of double quotes around the string you wish to use in your where clause.
For example:
=QUERY(D:E;"select E where D = ""this string's apostrophe breaks my code""";0)

Enclose the criteria in double quotes:
"select E where D = ""this string's apostrophe breaks my code"""

Related

REGEXMATCH on double quote character?

I'm trying to do a conditional formatting that matches on the double quote character followed by a zero. i.e.
"0 / 10" : this should match as true
"10 / 10": this should match as false
This regex is incorrect, as it matches on both:
=REGEXMATCH(B:B;"0 /")
I expect to be able to use the formula standard of escaping the " with an extra quote. It accepts this formula syntactically, but does not match:
=REGEXMATCH(B:B;"""0 /")
I tried matching with punctuation characters, no match:
=REGEXMATCH(B:B;"[[:punct:]]0 /")
I can use [digit[ to match the 10/10 case, but ~digit doesn't match the zero with a quote in front of it:
=REGEXMATCH(B:B;"[^[:digit:]]0 /")
I even tried concatenating the specific character, no match:
=REGEXMATCH(B:B;CONCATENATE(CHAR(34), "0 /"))
I'm very confused at this point. If I insert any other special character before the zero, I have no trouble matching it. But it seems like double-quote just isn't treated like a regular character somehow. Does anyone know what I'm doing wrong?
Try
=REGEXMATCH(B2;char(34)&"0 /")
Thanks to Tim for giving me a hint that lead to a solution. My problem was that the text in a cell was part of a formula. So I needed to extract the formula as text (FORMULATEXT) and then it works:
=REGEXMATCH(FORMULATEXT($B1);"""0 /")
Try this in row 1 of a different column:
=arrayformula(if(B:B<>"",iferror(regexmatch(B:B,"""0"),),))
if(B:B<>"" will only process the formula provided Col B has values.
iferror( will ignore numbers in Col B that produce #VALUE!.
"""0" is the regex. The double quote to the left of 0 is doubled up (or char(34)&"0" as per #Mike Steelson).

How can I make quotation marks act as normal text within a regex function?

I'm trying to use regex to extract information from a large text file on google sheets, but within the regex, I'm using quotation marks, and instead of treating everything like the text I want to use, the quotation marks make it so that the regex splits into many different parts. Is there some character I can add to prevent this?
As an example, say I used =REGEXEXTRACT("name"="",""name"="(\w+)"")
It would basically split this into:
REGEXEXTRACT(
"name"
=
""
,
""
name
"="
(\w+)
"")
and would return a formula parse error.
Is there any way I can cancel out certain quotation marks?
Solution:
You can escape double quotes by... another double quote!
So if your first formula argument is name"=" and your second formula argument is "name"="(\w+)", you would use:
=REGEXEXTRACT("name""=""","""name""=""(\w+)""")
Output: (note that I used concatenate to show the expressions)

Can't get simple regular expression to work in Google Sheets QUERY function

Google Sheets QUERY function does not seem to use regular expressions.
I want to match strings with an arbitrary number of spaces before the string. My QUERY function is:
=QUERY('$A$1:$B$2, "select B where A=' *abc'")
It returns #N/A
It works when my data does not have the leading blanks and the match string is just 'abc'. It's acting as if sheets has regular expressions disabled.
perhaps try like this:
=QUERY(A1:B2, "select B where A contains 'abc'")
if you need something more strict try:
=ARRAYFORMULA(IF(ISNA(REGEXEXTRACT(A1:A, "abc$")), , B1:B))

Regex to get all substrings before and after given set of words

I am trying to learn Regex and I have scenario where I thought I can use the same. I have a set of strings in the below format(as shown in the table) from which I need to extract each substring around joining operator "and", "or", "not". For eg:- "some column name1 = some value1" as one such substring from first string.
After that I need to extract left hand side string and right hand side string of operators "like", "=", "<", ">". In the above example it would give "some column name1" as one substring and "some value1" as another substring along with operator as "=".
some column name1 = some value1 and some column name2 < another value2 or some column name 3 > value3 not
column name 4 = value4 and name5 = value5
columnA = 324324
columnB like a text
value text
Since I am new to Regex, this is what I have tried till now but it doesn't seem to give me all the values around these operators. Once this works, I am thinking I can apply similar regex with operators as "like", "=", "<", ">" on the resulting substrings to get final output.
(.*?)\b(and|or|not)
When I try the above regex on the first example, this part "name5 = value5" is missing after matching.
(.+?)(and|or|not)(.+)
When I try this one, it matches the first substring but rest of them are matched as a single substring instead of splitting those again.
Please note that I was able to use split operation and give "and|or|not" as separator to get array of substrings however I am trying to see if I can directly get these matched substrings from the given string just for learning regex(This answer says it is possible to do using Regex). I have explored stackoverflow for similar questions but none of the solutions worked in my case. The language in my case is Objective C/Swift.
You may add an end of string anchor $ as an alternative to the delimiters.
(.*?)(?:\b(and|or|not)\b|$)
^^
See the regex demo.
If your string contains line breaks, you must make . match them by adding (?s), a DOTALL modifier, at the pattern start.

Google Sheets - Count Quotation Marks

Unfortunately the data I'm working with requires quotation marks, as it is entered into another system afterwards which requires it.
I'm trying to collect statistics, and need to count these entries. The only thing they have in common is they are wrapped in quotation marks.
I'm trying to use =COUNTIF($C3:$C, """) but it keeps changing it to =COUNTIF($C3:$C, "")"").
Is there a way to escape this so that i can count cells that contain a quotation mark (")?
Like Robin said, the most straightforward way to reference quotes is with the character code - for the double quote character it's CHAR(34). One possible solution to this uses QUERY(), like so:
=QUERY($C3:$C, "Select count(C) where C contains '" & CHAR(34) & "' label count(C) ''", 0)
You can omit the label count(C) '' at the end if you're okay with it displaying a header cell, but adding it on contains the entire result to a single cell.
Try this it seems to do what you want to do:
=COUNTIF(C3:C, "'*")
note the single quote mark.

Resources