I am working on the csv generation. I am seperating values which are seperated by comma(,). If the value in a field contains comma, then it should not seperate the field in excel. So I want to put a escape character there. I am using FasterCsv. So how I can put a escape character. What is the escape character of fastercsv?
Just quote every field (doublequotes by default) and commas inside of them are ignored:
CSV.generate(:col_sep=>',', :quote_char => '"') do |row|
row << ["Quid, quid", "latinum dictum"]
row << ["sit, altum", "viditur."]
end
=> "\"Quid, quid\",latinum dictum\n\"sit, altum\",viditur.\n"
If you have commas in your data, set a different column seperator with the :col_sep option. If you like your commas and can not live without them, set the data within quotation marks.
If you use the FasterCSV methods, this will be handled for you automatically!
a creative way is to replace the real comma with a look-alike. This may be stupid, it all depends on your use case. It was ok for us - I think. I need to post this before I change my mind, lol
my_string.gsub(',','‚')
I'm not sure if it copy pasted this correctly, but you can create it on mac by holding ALT(option) + ,
Related
I'm importing data from old spreadsheets into a database using rails.
I have one column that contains a list on each row, that are sometimes formatted as
first, second
and other times like this
third and fourth
So I wanted to split up this string into an array, delimiting either with a comma or with the word "and". I tried
my_string.split /\s?(\,|and)\s?/
Unfortunately, as the docs say:
If pattern contains groups, the respective matches will be returned in the array as well.
Which means that I get back an array that looks like
[
[0] "first"
[1] ", "
[2] "second"
]
Obviously only the zeroth and second elements are useful to me. What do you recommend as the neatest way of achieving what I'm trying to do?
You can instruct the regexp to not capture the group using ?:.
my_string.split(/\s?(?:\,|and)\s?/)
# => ["first", "second"]
As an aside note
into a database using rails.
Please note this has nothing to do with Rails, that's Ruby.
Given a loop like:
#For Each x In item.PostCategory
Dim cats = x.CategoryName & ", "
#Html.ActionLink(cats,
"PostsByCategory", "Posts", New With {.Category = x.CategoryName.ToSeoUrl,
.Page = Nothing}, Nothing)
Next
I need to remove only the last comma and space - everything I have tried removes the commas and spaces in the middle as well as the end. The loop renders categories and I want them separated by a comma and space but do not need or want the trailing comma and space. Each category needs to be a separate link so string.join won't work. I tried trim.substring - that removes the commas in the middle. TrimEnd did not work. I have searched and have not found a solution.
Instead of World, Science, - i want World, Science
You can try to check if x is the last item in PostCategory. If it is true, then append an empty string, else append comma and space :
Dim cats = x.CategoryName & IIf(x.Equals(item.PostCategory.Last()), "", ", ")
There's many different ways to solve this problem. You'll have to determine the best way. Simply, you can just not use a foreach and do a simple for instead. Then you can easily tell if you're on the last item by comparing the index with the count and conditional show or not show the comma based on that.
Alternatively, you can construct a list of the string values this code would otherwise render directly to the page and then use string.Join to join the list items separated by ", ". string.Join never appends the delimiter to the end, so that fixes your problem.
You could also go fancier with some sort of editor template or partial view or even create a HtmlHelper extension. If just depends on how you want to handle it.
I want to match emails separated by a comma like this :
'a#a.com, b#b.com, c#c.com'
I'm would like to reuse the regex defined in the Devise gem :
/\A[^#\s]+#([^#\s]+\.)+[^#\s]+\z/
I know that I won't be RFC2822 compliant but I don't care because I only need a very simple email validation.
My attempt to match the emails separated by commas is the following :
/\A(([^#\s]+#([^#\s]+\.)+[^#\s]+),)*\s([^#\s]+#([^#\s]+\.)+[^#\s]+)\z/
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
everything like 'a#a.com,' spaces last element 'c#c.com'
many times
Unfortunately this is not working as expected. It is matching string like this (note the trailing comma) :
a#a.com, b#b.com, c#c.com,
Could someone help me with this please?
You are only matching the space after the last comma, move the \s to the group that is repeating. Also as Platinum Azure mentioned [^#\s] includes commas you can avoid this by changing it to [^#\s,]
/\A(([^#\s]+#([^#\s]+\.)+[^#\s,]+),\s)*([^#\s]+#([^#\s,]+\.)+[^#\s,]+)\z/
The serialize method allows an object like a hash to be stored in the database as a YAML string. However, I recently found myself wanting to have a text field to allow a user to input their own string and have the controller create a hash off of that string.
<%= f.text_field :yaml, :value => '--- \nlast_name: Smith\nfirst_name: Joe\n' %>
Yes, I want single quotes: I want to preserve the \n in the display. But the problem is that, as a result, the resultant string object gets escaped:
--- \\nlast_name: Smith\\nfirst_name: Joe\\n
I run the string through two regexes: The first replaces the double backslash with a single backslash. Then next converts \n (two characters) into \n (special single character).
So in my controller:
yhash = YAML.load(params[:form][:yaml].gsub(/\\\\/, "\\").gsub(/\\n/, "\n"))
This now works, but seems awfully convoluted. Is there a more elegant way for a user to submit yaml?
Are you saying you want the users to be able to write \n to mark the newlines in the yaml they are entering? Because in that case the way you are doing it now with regexps is very straightforward.
The escape sequence \n is a feature of Ruby strings. If you want to implement it in your web form yaml interface too, a regexp is a valid way to do that.
Are you sure you have to do the double-slash replacement? I think it should only show up as double-slash if you do .inspect.
I want to use validates_format_of to validate a comma separated string with only letters (small and caps), and numbers.
So.
example1, example2, 22example44, ex24
not:
^&*, <> , asfasfsdafas<#%$#
Basically I want to have users enter comma separated words(incl numbers) without special characters.
I'll use it to validate tags from acts_as_taggable_on. (i don't want to be a valid tag for example.
Thanks in advance.
You can always test out regular expressions at rubular, you would find that both tiftiks and Tims regular expressions work albeit with some strange edge cases with whitespace.
Tim's solution can be extended to include leading and trailing whitespace and that should then do what you want as follows :-
^\s*[A-Za-z0-9]+(\s*,\s*[A-Za-z0-9]+)*\s*$
Presumably when you have validated the input string you will want to turn it into an array of tags to iterate over. You can do this as follows :-
array_var = string_var.delete(' ').split(',')
^([a-zA-Z0-9]+,\s*)*[a-zA-Z0-9]+$
Note that this regex doesn't match values with whitespace, so it won't match multiple words like "abc xyz, fgh qwe". It matches any amount of whitespace after commas. You might not need ^ or $ if validates_format_of tries to match the whole string, I've never used Rails so I don't know about that.
^[A-Za-z0-9]+([ \t]*,[ \t]*[A-Za-z0-9]+)*$
should match a CSV line that only contains those characters, whether it's just one value or many.