I am trying to capture a conditional of years in RegEx. Basically, if they implement just a two digit year, I want to make it a four digit year. So if they put :
1/2/08
I want to make it :
1/2/2008
Any ideas?
One [pretty nasty] way using regex:
"1/2/08".sub! /\/(\d{2})$/, '/20\1'
Wouldn't it be better to just parse the string into a date object, though? Then you can treat it as a date properly! :)
You could split on '/' and if the last component has a length of two you prepend 20 and then assemble the date again.
You could split the string up using
(.*/)(..)$
and then substitute with something like
$120$2
to put the string back together (tested with http://www.regexplanet.com/simple/).
You might need to think about what you want to happen for dates in the 20th century - this approach will recognise 21/01/98 as 21/01/2098 which might not be what you want... It might be better to parse the string out properly rather than just blindly regex it!
Related
I have a UniCode string UniStr.
I also have a MAP of { UniCodeChar : otherMappedStrs }
I need the 'otherMappedStrs' version of UniStr.
Eg: UniStr = 'ABC', MAP = { 'A':'233','B':'#$','C':'9ij' }, Result = '233#$9ij'
I have come up with the formula below which works;
=ArrayFormula(JOIN("",VLOOKUP(REGEXEXTRACT(A1,REPT("(.)",LEN(A1))),MapRange,2,FALSE)))
The MAP being a whole character set (40 chars) is quite large.
I need to use this function in multiple spreadsheets. How can I subsume the MAP into the formula for portability ?
Is there a better way to iterate a string other than the REGEXEXTRACT method in formula ? This method has limitation for long strings.
I also tested the below formula. Problem here is it gives 2 results (or the size of the array within SUBSTITUTE replacement). If 3 substitutions made, then it gives three results. Can this be resolved ?
=ArrayFormula(SUBSTITUTE(A1,{"s","i"},{"#","#"}))
EDIT;
#Tom 's first solution appears best for my case (1) REGEX has an upper limit on search criteria which does not hinder in your solution (2) Feels fast (did not do empirical testing) (3) This is a better way to iterate string characters, I believe (you answered my Q2 - thanks)
I digress here. I wish google would introduce Named-Formulas or Formula-Aliases. In this case, hypothetically below. I have sent feed back along those lines many times. Nothing :(
MyFormula($str) == ArrayFormula(join(,vlookup(mid($str,row(indirect("1:"&len($str))),1), { "A","233";"B","#$";"C","9ij" },2,false)))
Not sure how long you want your strings to be, but the more traditional
=ArrayFormula(join(,vlookup(mid(A1,row(indirect("1:"&len(A1))),1), { "A","233";"B","#$";"C","9ij" },2,false)))
seems a bit more robust for long strings.
For a more radical idea, supposing the maximum length of your otherMappedStrings is 3 characters, then you could try:
=ArrayFormula(join(,trim(mid("233 #$9ij",find(mid(A1,row(indirect("1:"&len(A1))),1), "ABC")*3-2,3))))
where I have put a space in before #$ to pad it out to 3 characters.
Incidentally the original VLOOKUP is not case sensitive. If you want this behaviour, use SEARCH instead of FIND.
You seem to have several different Qs, but considering only portability, perhaps something like the following would help:
=join(,switch(arrayformula(regexextract(A1&"",rept("(.)",len(A1)))),"A",233,"B","#$","C","9ij"))
extended with 37 more pairs.
I have a string eg. String-aa. I want to be able to capitalise every letter after the "-". Is there a straightforward was of achieving this?
I am aware of .capitalize however dont know how to implement for this particular requirement.
You can make use of block form of gsub
"String-aa-bbbb".gsub(/-\w+/){|e| e.upcase}
#=> "String-AA-BBBB"
The above code will capture a letter followed by - and will capitalize it
I have a calculator-like with obviously number buttons and a . button.
How can I display it depending on the local used knowing it's in the end more like a string than a decimal, so I guess I cannot use NSNumberFormatter for that.
Is there a way to do it without "translating" it for every language that I will use (which will be way less than there are locales)?
I thought of creating a formatted number like 0.1 using the locale, format it into string and keep only the . it its locale version but I guess there are proper ways
Thanks
Here is a way for it to work, I don't know if it's the best way...
let decimalSeparator = NSLocale.currentLocale().objectForKey(NSLocaleDecimalSeparator) as? String
I have a field -
:Revenue
and it should accept values like 10,000.00, but if I input such value it stores 10 into database instead of 10000.00
What should I do to strip of commas before I save?
I've tried to find a few solutions online but wasn't able to implement them as I found them incomplete. If someone could help me I would really appreciate it.
**The problem now I am facing is that as soon as I enter the value rails converts string in to float value before it can run the gsub function, like if I enter 50,000.00 its converting into float 50.0 before calling the gsub, is there any way to over the to_f method which rails is calling on the string.
Removing commas is pretty simple:
value.gsub(/,/, '').to_f
Keep in mind that European formatting often uses comma as the decimal value separator so your results would be off by a factor of 100 if processing those sorts of numbers.
You can take a String#delete.
"10,000,000.00".delete(',').to_f
# => 10000000.0
I found the solution after looking at few places and combining few solutions, since I had to use gsub before the linking to model has to be done. so I created the method in my controller and called it before create and update action. and wrote the following code in the method
params[:record][:Revenue] = params[:record][:Revenue].gsub(/,/,"")
I'm writing a Fitnesse test for a web application. One of the items to test is a drop-down box, whose value is determined by the current date, in DD/MM/YYYY format.
I'd thought that using the !today variable in the Fitnesse suite might be a useful way of setting a variable, but I've run into the problem that Fitnesse expresses the date as (for example) 11 Mar, 2011, where I need 11/03/2011. I can get the date in numberic format using the -xml modifier, but I'm still left with a pretty huge string like 2011-03-11T05:51:22.
Is there a way of getting substrings of this, and then piping those into page variables, or am I barking up entirely the wrong tree here?
Thanks!
!today (MM/dd/yyyy) produces 09/17/2012. You can use any format codes you like. It uses the SimpleDateFormat class.
Well, it turns out not entirely the wrong tree :-)
For reference, the !today function has a few other methods, and you can use them to gather individual sections of the date as necessary:
!today (dd) - gives the day of the month, in numeric form
!today (MM) - gives the month of the year, in numeric form
!today (yyyy) - gives the year, in numeric form
There are a few others, but all I ended up using were these. Combine them as necessary, and Robert is your mother's brother, as it were...
This will get you the date you require in Test Cases written in Fitnesse Wiki
${=!today (ddMMyyyy)=}
The ! (Exclamation mark) is interpreted literally, so symbols like !today are not expanded. You can use a plain table:
|class name|
|!today (MM/dd/yyyy)|