search all matching patterns in excel 2003 and replace - excel-2003

I am basically looking for an excel based formula that can look in the cell value and replace all instances of ;# with a space or "; ". For the start, I know search replace functionality, I know macros can do that, the challenge is that I can not use either of the two methods.
The data appears in the following format:
;#Business Mix;#Improve Productivity;#
;#Distribution;#Improve Productivity;#
;#Distribution;#Improve Productivity;#
;#Risk Selection;#Business Mix;#Improve Productivity;#
;#Risk Selection;#Business Mix;#Improve Productivity;#
;#Risk Selection;#Business Mix;#Improve Productivity;#
;#Distribution;#Improve Productivity;#
;#Distribution;#Improve Productivity;#
;#Distribution;#Improve Productivity;#
;#Distribution;#Improve Productivity;#
;#Distribution;#Improve Productivity;#
;#Distribution;#Improve Productivity;#
;#Distribution;#Improve Productivity;#
I am currently using the following formula to arrive at the solution but does not work perfectly:
=REPLACE(REPLACE(LEFT(REPLACE(VLOOKUP($D$2,all,3,FALSE),3,2,""),LEN(REPLACE(VLOOKUP(D2,all,3,FALSE),3,2,""))-2),FIND(";#",LEFT(REPLACE(VLOOKUP(D2,all,3,FALSE),3,2,""),LEN(REPLACE(VLOOKUP(D2,all,3,FALSE),3,2,""))-2),1),2,"; "),FIND(";#",REPLACE(LEFT(REPLACE(VLOOKUP(D2,all,3,FALSE),3,2,""),LEN(REPLACE(VLOOKUP(D2,all,3,FALSE),3,2,""))-2),FIND(";#",LEFT(REPLACE(VLOOKUP(D2,all,3,FALSE),3,2,""),LEN(REPLACE(VLOOKUP(D2,all,3,FALSE),3,2,""))-2),1),2,"; "),1),2,"; ")
all = named range
So we could have one item
;#Business Mix;#
or
;#Business Mix;#Improve Productivity;#
or more than two or three or four.
The end result should have only the texts separated by ; and if there is only one selection e.g. Business mix, if should have no ;.
Any help will be much appreciated.
Thanks

Use Text to Columns with # as the delimiter. Stitch the columns back together, say in D1, with =CONCATENATE (say =E1&F1&G1...) copied down to suit, copy the results and Paste Special Values over the top, add
=IF(RIGHT(D1,1)=";",LEFT(D1,LEN(D1)-1),D1)
in Row1 of a spare column and copy down to suit. Copy all and Paste Special Values over the top again. Delete excess.

Related

Rails, Postgres 12, Query where pattern matches regex, and contains substring

I have a field in the database which contains strings that look like: 58XBF2022L1001390 I need to be able to query results which match the last letter(in this case 'L'), and match or resemble the last four digits.
The regular expression I've been using to find records which match the structure is: \d{2}[A-Z]{3}\d{4}[A-Z]\d{7}, So far I've tried using a scope to refine the results, but I'm not getting any results. Here's my scope
def self.filter_by_shortcode(input)
q = input
starting = q.slice!(0)
ending = q
where("field ~* ?", "\d{2}[A-Z]{3}\d{4}/[#{starting}]/\d{3}[#{ending}]\g")
end
Here are some more example strings, and the substring that we would be looking for. Not every string stored in this database field matches this format, so we would need to be able to first match the string using the regex provided, then search by substring.
36GOD8837G6154231
G4231
13WLF8997V2119371
V9371
78FCY5027V4561374
V1374
06RNW7194P2075353
P5353
57RQN0368Y9090704
Y0704
edit: added some more examples as well as substrings that we would need to search by.
I do not know Rails, but the SQL for what you want is relative simple. Since your string if fixed format, once that format is validated, simple concatenation of sub-strings gives your desired result.
with base(target, goal) as
( values ('36GOD8837G6154231', 'G4231')
, ('13WLF8997V2119371', 'V9371')
, ('78FCY5027V4561374', 'V1374')
, ('06RNW7194P2075353', 'P5353')
, ('57RQN0368Y9090704', 'Y0704')
)
select substr(target,10,1) || substr(target,14,4) target, goal
from base
where target ~ '^\d{2}[A-Z]{3}\d{4}[A-Z]\d{7}$';

How to scrape a page for certain strings from an array of substrings with Nokogiri

I want to scrape a restaurant page for certain titles of dishes.
I created an array holding keywords:
myarray = {"Rice", "Soup", "Chicken", "Vegetables"}
Whenever one of those keywords is found in a webpage, my scraper is supposed to give me the entire dish-title. I made this work with the following code:
html_doc = Nokogiri::HTML.parse(browser.html)
word = html_doc.at(':contains("Rice"):not(:has(:contains("Rice")))').text.strip
puts word
For example this returns: "Dish 41 - Vegetables with Chicken and Rice"
The problem is that the above code stops after the first dish is found. It does not loop through all dish-titles containing the word rice.
Secondly, I do not know how to let the code check for an entire array of substrings.
Use css. This will find all the elements which matches the given CSS and give you the collection:
words = html_doc.css(':contains("Rice"):not(:has(:contains("Rice")))').map(&:text)
I solved the second part of my question myself with this:
word = html_doc.css(":contains('#{keyword}'):not(:has(:contains('#{keyword}')))").map(&:text)

How to print each line containing regex pattern and sort them alphabetically?

I have a test_list.txt file containing lines of file names. Each file name contains the date when they were created. Here's how it looks like:
test_list.txt:
UTF_06012018_SAMPLE_Control.xlsx
UTF_06022018_SAMPLE_Control.xlsx
UTF_06092018_SAMPLE_Control.xlsx
UTF_06022018_SAMPLE_Control.xlsx
UTF_06082018_SAMPLE_Control.xlsx
UTF_06032018_SAMPLE_Demand.xlsx
UTF_06092018_SAMPLE_Demand.xlsx
UTF_06122018_SAMPLE_Demand.xlsx
UTF_06032018_SAMPLE_Control.xlsx
UTF_06022018_SAMPLE_Demand.xlsx
The date in the file name is in the format mmddyyyy. Also, there are files which were created on the same date. What I'm trying to do is to print the line that matches the regex expression for the dates and sort them alphabetically by date.
Here's my code so far:
path = Dir.glob('/path/to/my/file/*.txt').first
regex = /(\d{1,2}\d{1,2}\d{4})/
samplefile = File.open(path)
string = File.read(samplefile)
string.scan(regex).each do|x|
sorted = x.sort_by { |s| s.scan(/\d+/).first.to_i }
puts sorted
end
However, what my code does is it only prints the dates, not the entire line. To add to that, it doesn't even sort them alphabetically. How to tweak it and make it do as I intend to?
You may use
string.scan(/^([^_]*_(\d++)(.*))/).sort_by { |m,n,z| [n.to_i,z] }.collect{ |m,n,z| m}.join("\n")
See the Ruby demo.
The regex will extract all lines into a three element array with the following values: whole line, the date string, and the string after the date. Then, .sort_by { |m,n,z| [n.to_i,z] } will sort by the date string first, and then by the substring after the date. The .collect{ |m,n,z| m} will only keep the first value of the array elements and .join("\n") will re-build the resulting string.
Note that instead of [n.to_i,z], you might want to parse the date string first, then use [Date.strptime(n,"%d%m%Y"),z] (add require 'date').
Regex details
^ - start of a line
([^_]*_(\d++)(.*)) - Group 1 (m): the whole line meeting the following patterns:
[^_]* - zero or more chars other than _
_ - an underscore
(\d++) - Group 2 (n): 1+ digits, a possessive match
(.*) - Group 3 (z): the rest of the line.

Postgresql text searching, matching multiple words

I don't know the name for this kind of search, but I see that it's getting pretty common.
Let's say I have records with the following file names:
'order_spec.rb', 'order.sass', 'orders_controller_spec.rb'
If I search with the following string 'oc' I would like the result to return 'orders_controller_spec.rb' due to match the o in orders and the c in controller.
If the string is 'os' then I'd like all 3 to match, 'order_spec.rb', 'order.sass', 'orders_controller_spec.rb'.
If the string is 'oco' then I'd like 'orders_controller_spec.rb'
What is the name for this kind of search and how would I go about getting this done in Postgresql?
This is a called a subsequence search. One simple way to do it in Postgres is to use the LIKE operator (or several of the other options in those docs) and fill the spaces between your letters with a wildcard, which for LIKE is %. To match anything with an o followed by an s in the words column, that would look like this:
SELECT * FROM table WHERE words LIKE '%o%s%';
This is a relatively expensive search, but you can improve performance with a varchar_pattern_ops or text_pattern_ops index to support faster pattern matching.
CREATE INDEX pattern_index ON table (words varchar_pattern_ops);

Most efficent way to get a list of strings with space delimiter

A project can have many tags. When editing a project I'd like to list all tags in a input field (stackoverflow style). In Rails 3 I have the following code, where I push all my names into an array before calling join(' ') but is there a quicker / more elegant way?
#tags = #project.tags
#tags_array = []
#tags.each do |tag|
#tags_array << tag.name
end
#tags_string = #tags_array.join(' ')
Maybe what you want is the Enumerable#collect method:
#tags_string = #project.tags.collect(&:name).join(' ')
Collect comes in handy when you're trying to transform one list into another list of equal size, which is exactly the pattern here.
The &:name part means "call method name on the given object" and is something that could be spelled out as { |t| t.name } equivalently.
The Enumerable library is really great and you should have a look through it and be familiar with the various methods as it can save you a ton of time.
One way is to use threads while iterating through the #tags array. Have a look at this excellent article http://rubysource.com/threading-ruby/
Secondly it seems #tag is an array
I would use map to return an array of names and then join them to a string
names = #tags.map{|tag| tag.name}.join(' ')

Resources