Write a regular expression for the following:
A regular expression that finds dates in files. Correct dates must be in the format
DD-MM-YYYY and within the years 1900s and 2000s. No need to enforce the
correct maximum number of days in a month.
For example, within the following list:
10-02-2011
, 6-Feb-2016
, 8-5-2016
, 07-08-1966
, 32-05-2022
only 10-02-2011 and 07-08-1966 are valid dates.
A regular expression that accepts any of the following answers for the question
“What are 5, 6 and 14?”
numbers
They’re numbers
They are numbers
According to your question, the regex pattern must match any date between 1900 and 2000, is that correct? If it is that what you want, this is the pattern you need:
(3[0-1])|([0-2][0-9])-((0[0-9])|(1[0-2]))-(19\d{2})|2000
The year group will match any year from 1900 to 2000, let me know if you find any error on this pattern, or give me some entries that this pattern should've matched...
Hope I have helped!
Related
Here is my data:
I am trying to build a SUMIFS formula to sum the sessions, if the month = "last month" (i.e., parsed out of these strings), and the Channel Grouping = "Display".
Here's what I have so far:
=SUMIFS(H3:H,F3:F,________,G3:G,"Direct")
Since this is a string, not a date, I am not sure how to get it to match "last month".
Why not build up a string like this (or just hard-code it?)
=sumifs(H3:H,F3:F,year(today())&"|"&text(month(today())-1,"00"),G3:G,"Direct")
This builds up a string equal to "2017|03" by taking the year from today's date (2017) and one less than the month number from today's date which at time of writing is April so 4-1=3. The text function formats it with a leading zero. So the whole thing is"2017" & "|" & "03" which gives "2017|03" - this is compared against column F.
Note: January would be a special case (existing formula would give "2018|00" for previous month to January 2018 so would need a bit of extra code to cover this case and make it fully automatic).
By 'hard-code it' I mean just put 2017|03 in as a literal string like this
=sumifs(H3:H,F3:F,"2017|03",G3:G,"Direct")
then just change it manually for different months.
Here is a more general formula
=sumifs(H3:H,F3:F,year(eomonth(today(),-1))&"|"&text(month(eomonth(today(),-1)),"00"),G3:G,"Direct")
Just change the -1 to -2 etc. for different numbers of months.
EDIT
In light of #Max Makhrov's answer, this can be shortened significantly to
=sumifs(H3:H,F3:F,text(eomonth(today(),-1),"YYYY|MM"),G3:G,"Direct")
I would like to add two more options:
1
This formula is slightly shorter and more powerrful, because it gives the full control over date format:
=TEXT(TODAY(),"YYYY|MM")
formula syntax is here:
https://support.google.com/docs/answer/3094139?hl=en
2
In your case converting date to string is more efficient because it calculates one time in the formula, so there's fewer calculations. But sometimes you need to convert text into date. In this case I prefer using regular expresions:
=JOIN("/",{REGEXEXTRACT("2017|03","(\d{4})\|(\d{2})"),1})*1
How it works
REGEXEXTRACT("2017|03","(\d{4})\|(\d{2})") gives 2 separate cells output:
2017 03
{..., 1} adds 1 to ... and adds it to the right:
2017 03 1
JOIN("/", ...) joins the ... input:
2017/03/1
This looks like date, but to make it real date, multimpy it by 1:
"2017/03/1"*1 converts string that looks like date into a number 42795 which is serial number for date 2017 march 01
I want to compare string representations of weeks, e.g. week "01/17" is before "02/17" and after "52/16".
The following code throws an exception, I guess because my string doesn't hint at the exact day of each week. However, I don't care - it could all be Mondays or Thursdays or whatever:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("ww/YY", Locale.GERMANY);
LocalDate date1 = formatter.parse(str1, LocalDate::from);
Do I need to modify the parser? Or parse to some other format? Unfortunatley there is no object like YearMonth for weeks...
One solution would be to always default to the same day, say the Monday. You could build a custom formatter for that:
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
.appendPattern("ww/YY")
.parseDefaulting(ChronoField.DAY_OF_WEEK, 1)
.toFormatter(Locale.GERMANY);
You can now build LocalDates representing the Monday of the given week:
LocalDate d1 = LocalDate.parse("01/17", fmt);
LocalDate d2 = LocalDate.parse("52/16", fmt);
System.out.println(d1.isAfter(d2));
which prints true because 01/17 is after 52/16.
I wasn't able to find a way for this to work with the DateTimeFormatter class, but I would like to suggest a different approach.
The Threeten Extra library contains a number of classes that were deemed too specific to include in the java.time library. One of them is the YearWeek class you mention.
Your problem can be solved by parsing the week-number and year manually from the input-string and then invoking the YearWeek creator-method like this:
YearWeek yw = YearWeek.of(year, monthOfYear);
tl;dr
YearWeek.parse( "2017-W01" )
ISO 8601
Or parse to some other format?
Yes, use another format.
Use the standard ISO 8601 formats when serializing date-time values to text. The standard includes support for week dates.
For a year-week that would be four year digits, a hyphen, a W, and two digits for the week of the year.
2017-W01
Get clear on your definition of a “week”. The ISO 8601 definition is that:
The Week # 1 contains the first Thursday of the year, and
Runs Monday-Sunday.
So years run either 52 or 53 weeks long. And note that under this definition, the first few days of the year may be in the prior year when week-numbering. Likewise, the last few days of the year may be in the following year when week-numbering.
If you want to indicate a particular day within that week, append a hyphen and a single digit running 1-7 for Monday-Sunday.
Tip: To see ISO 8601 week numbers by default on your computer, you may need to adjust your OS setting. For example, on macOS set System Preferences > Language & Region > Calendar > ISO 8601 to make apps such as Calendar.app to display week numbers with this standard definition.
2017-W01-7
By the way, a couple of similar representations:
An ordinal date meaning the year and the day-of-year-number running from 1-366 is year, a hyphen, and a three-digit number: 2017-123
Month-Day without year is two hyphens, month number, hyphen, and day-of-month number: --01-07
Note that the use of Locale as seen in the Question is irrelevant here with the standard ISO 8601 formats.
YearWeek
Unfortunatley there is no object like YearMonth for weeks...
Ahhh, but there is such a class.
For a class to directly represent the idea of a week-year, see the correct Answer by Henrik. That Answer shows the ThreeTen-Extra library’s class YearWeek.
The YearWeek class can directly parse and generate strings in standard format.
YearWeek yw = YearWeek.parse( "2017-W01" );
You can compare the YearWeek objects with methods: compareTo, equals, isBefore, isAfter.
yw.isBefore( thatYw )
The ThreeTen-Extra project offers other classes such as YearQuarter that you may find useful.
I want to store date in my Postgres database.
The only problem is that this date can have optional day or even month.
Example:
User provides time period when he was employed - not necessary full date (day + month + year), but only start year and end year.
However there are users, who worked only from may to october in the same year so month have to be provided too.
How to handle this kind of optional date parts?
Use a proper date type anyway. Do not store text or multiple columns. That would be more expensive and less reliable.
Use the function to_date(), which is fit to deal with your requirements out of the box. For instance, if you call it with a pattern 'YYYYMMDD' and the actual string is missing characters for day, or month and day, it defaults to the first month / day of the year / month:
db=# SELECT to_date('2001', 'YYYYMMDD');
to_date
------------
2001-01-01
db=# SELECT to_date('200103', 'YYYYMMDD');
to_date
------------
2001-03-01
You could store a precision flag indicating year / month / day in addition if you need that.
While the accepted answer is a good one, there is another alternative.
ISO 8601
The ISO 8601 standard defines sensible formats for textual representations of various kinds of date-time values.
A year is represented in the obvious manner, a four-digit number: 2014
A year-month is represented with a required hyphen: 2014-01Note that in other ISO 8601 formats, the hyphen is optional. But not for year month, to avoid ambiguity.
A full date is similar: 2014-08-21 or without optional hyphens: 20140821. I recommend keeping the hyphens.
So you could store the values as text. The length of text would tell you whether it is year-only, year-month, or date.
I'm using Rails and MongoMapper as my working platform.
I want to generate a custom key with the help of month and year. The possible format would be YYYYMM####,
YYYY is current YEAR which I can get as Date.today.strftime("%Y")
MM is current Month Which I can get as Date.tody.strftime("%m")
After that ### is incremented integer value
I get the last job with the code
jobForLastnum = Job.last(:order => :_id.desc)
lastJobNum = jobForLastnum.job_number
Now my question is I received the job_number as '201305100'
I want to split it with custom length like, ['2013','05','100']
I know how to split a string in ruby and I successfully did that but i got result as individual character like
['2','0','1','3','0','5','1','0','0']
With the help of this I could retrieve the year:
lastJobNum.to_s[0,4]
With the help of this I got the month:
lastJobNum.to_s[4,2]
But after that there is custom length string. How can I get all the data in a single array?
You can simply use ranges:
c = "2013121003"
[c[0..3], c[4..5], c[6..-1]]
You can also use String#unpack:
"20131210034".unpack("A4A2A*")
Or with regexp as suggested by tessi, using String#scan:
c = "2013121003"
c.scan(/(\d{4})(\d{2})(\d+)/)
In all cases, this will return an array with the year, month, and job id as strings.
A regexp can help you here.
jobNumber = 201305100
year, month, job_id = jobNumber.to_s.match(/(\d{4})(\d{2})(\d*)/)[1..3]
First, we convert the jobNumber to a String. Then we throw a regexp at it. The regexp has three capture groups ((\d{4}) four numbers for the year, (\d{2}) two numbers for the month, (\d*) any remaining number for the job_id).
The job_number.to_s.match(...) returns a MatchData object, which we can access by its first three capture groups with [1..3] (see the documentation).
Finally, we assign the resulting Array to our variables year, month, and job_id.
year
#=> 2013
month
#=> 05
job_id
#=> 100
I have an odd issue with an NSDateFormatter, I am passing the following string as a date format "dd/MM/yy"
If I enter 50 for the year I get a conversion to 1950 however anything below that for instance 49 results in 2049. Any ideas how I can remedy this?
Many thanks.
It sounds like you'll need to force a four digit response (or programmatically prepend two digits of "19") to wherever you're drawing your string from. Lots of people are using dates in the near to mid-term future like "12/21/12" (end of the Mayan Calendar era) so it's natural that a 2 digit year assumes 2000+ for digits 1-50 and 1999- for digits (50-99).
I'm also seeing a number of Google hits on the keyword terms "NSDateFormatter" & "century", b.t.w.