how to make Google sheets REGEXREPLACE return empty string if it fails? - google-sheets-formula

I'm using the REGEXREPLACE function in google sheets to extract some text contained in skus. If it doesn't find a text, it seems to return the entire original string. Is it possible to make it return an empty string instead? Having problems with this because it doesn't actually error out, so using iserror doesn't seem to work.
Example: my sku SHOULD contain 5 separate groups delimited by the underscore character '_'. in this example it is missing the last group, so it returns the entire original string.
LDSS0107_SS-WH_5
=REGEXREPLACE($A3,"[^_]+_[^_]+_[^_]+_[^_]+_(.*)","$1")
Fails to find the fifth capture group, that is correct... but I need it to give me an empty string when it fails... presently gives me the whole original string. Any ideas??

Perhaps the solution would be to add the missing groups:
=REGEXREPLACE($A1&REPT("_ ",4-(LEN($A1)-LEN(SUBSTITUTE($A1,"_","")))),"[^_]+_[^_]+_[^_]+_[^_]+_(.*)","$1")
This returns space as result for missing group. If you don't want to, use TRIM function.

Related

xpath search from importxml function in google sheets

how do I get "Div/yield" value from here? i've tried //td[node()='Div/yield' and //td[text()='Div/yield'.
and //td[#data-snapfield='latest_dividend-dividend_yield']/following-sibling::td
#sideshowbarker is correct in that there's a newline at the end so looking for an element with the exact text would return 0 results. Another way to do this (one is through #sideshowbarker's answer) is to look for an element that contains this text. So the first step is:
//td[contains(text(),'Div/yield')]
But you don't need this. Your last answer is on the right track. You've identified the element that you're after, but I think you're looking for the text. So you need to add text() at the end:
//td[#data-snapfield='latest_dividend-dividend_yield']/following-sibling::td/text()
But if you want to use the field name, so you could use the xpath for the other fields as well, then just combine these:
//td[contains(text(),'Field name')]/following-sibling::td/text()
Now just replace Field name with the field you're after..
e.g. 'Div/yield': //td[contains(text(),'Div/yield')]/following-sibling::td/text()

return line of strings between two strings in a ruby variable

I would like to extract a line of strings but am having difficulties using the correct RegEx. Any help would be appreciated.
String to extract: KSEA 122053Z 21008KT 10SM FEW020 SCT250 17/08 A3044 RMK AO2 SLP313 T01720083 50005
For Some reason StackOverflow wont let me cut and paste the XML data here since it includes "<>" characters. Basically I am trying to extract data between "raw_text" ... "/raw_text" from a xml that will always be formatted like the following: http://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&hoursBeforeNow=3&mostRecent=true&stationString=PHNL%20KSEA
However, the Station name, in this case "KSEA" will not always be the same. It will change based on user input into a search variable.
Thanks In advance
if I can assume that every strings that you want starts with KSEA, then the answer would be:
.*(KSEA.*?)KSEA.*
using ? would let .* match as less as possible.

XPath & Replace Exact Match

can anyone help me with this?
I'm trying to change fields that have a "0" to Nothing.
I have this:
[str_replace("0",",{price[1]/sale[1]})]
but I think this will replace all fields that "contain" a 0 so for example if it had 301 then it would become 31. What I want is to only replace fields that only contain a single 0 to nothing.
How can I do this? (the code needs to be on a single line as it's run on a form field)
A simple way to do this may be as follows (not tested!):
[{string(price[1][number(sale[1]) != 0]/sale[1])}]
This expression includes an additional condition to only find the number if it is not 0 in the first place. If found it returns the number, otherwise it will return an empty string since the function string of an empty node set should yield exactly this.

How do I filter file names out of a SQLite dump?

I'm trying to filter out all file names from an SQLite text dump using Ruby. I'm not very handy/familiar with regex and need a way to read, and write to a file, another dump of image files that are within the SQLite dump. I can filter out everything except stuff like this:
VALUES(3,5,1,43,'/images/e/e5/Folder%2FOrders%2FFinding_Orders%2FView_orders3.JPG','1415',NULL);
and this:
src="/images/9/94/folder%2FGraph.JPG"
I can't figure out the easiest way to filter through this. I've tried using split and other functions, but instead of splitting the string into an array by the character specified, it just removed the character.
You should be able to use .gsub('%2', ' ') the %2 with a space, while quoted, it should be fine.
Split does remove the character that is being split, though. So you may not want to do that, or if you do, you may want to use the Array#join method with the argument of the character you split with to put it back in.
I want to 'extract' the file name from the statements above. Say I have src="/images/9/94/folder%2FGraph.JPG", I want folder%2FGraph.JPG to be extracted out.
If you want to extract what is inside the src parameter:
foo = 'src="/images/9/94/folder%2FGraph.JPG"'
foo[/^src="(.+)"/, 1]
=> "/images/9/94/folder%2FGraph.JPG"
That returns a string without the surrounding parenthesis.
Here's how to do the first one:
bar = "VALUES(3,5,1,43,'/images/e/e5/Folder%2FOrders%2FFinding_Orders%2FView_orders3.JPG','1415',NULL);"
bar.split(',')[4][1..-2]
=> "/images/e/e5/Folder%2FOrders%2FFinding_Orders%2FView_orders3.JPG"
Not everything in programming is a regex problem. Somethings, actually, in my opinion, most things, are not candidates for a pattern. For instance, the first example could be written:
foo.split('=')[1][1..-2]
and the second:
bar[/'(.+?)'/, 1]
The idea is to use whichever is most clean and clear and understandable.
If all you want is the filename, then use a method designed to return only the filename.
Use one of the above and pass its output to File.basename. Filename.basename returns only the filename and extension.

Using Regex to grab multiple values from a string and drop them into an array?

Trying to grab the two $ values and the X value from this string in Ruby/watir:
16.67%: $xxx.xx down, includes the Policy Fee, and x installments of $xxx.xx
So far I've got:
16.67%:\s+\$(\d+.\d{2})
which grabs the first xxx.xx fine, what do I need to add to it to grab the last two variables and load this all into an array?
You can use the following, but regex may be unnecessary if the surrounding text is always the same:
\$(\d+.\d{2}).*?(\d+) installments.*?\$(\d+.\d{2})
http://www.rubular.com/r/sk5wO3fyZF
if you know that the text in between will always be the same you could just:
16.67%:\s+\$(\d+.\d{2}) down, includes the Policy Fee, and x installments of (\d+.\d{2})
You better use scan.
sub(/.*%/, '').scan(/\$?([\d\.]+)/)
Have you considered just splitting the string on the $ character?, then manipulating what you get with a regex or basic string commands?
/\$(\d+.\d{2}).+\$(\d+.\d{2})/ should do it. it wont matter what text is there, only that there are two "$" in the sentence.

Resources