English 中文(简体)
Check if string is of SortableDateTimePattern format
原标题:

Is there any way I can easily check if a string conforms to the SortableDateTimePattern ("s"), or do I need to write a regular expression?

I ve got a form where users can input a copyright date (as a string), and these are the allowed formats:

Year: YYYY (eg 1997)

Year and month: YYYY-MM (eg 1997-07)

Complete date: YYYY-MM-DD (eg 1997-07-16)

Complete date plus hours and minutes: YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)

Complete date plus hours, minutes and seconds: YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)

Complete date plus hours, minutes, seconds and a decimal fraction of a second YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)

I don t have much experience of writing regular expressions so if there s an easier way of doing it I d be very grateful!

最佳回答

Not thoroughly tested and hence not foolproof, but the following seems to work:

var regex:RegExp = /(?<=s|^)d{4}(-d{2}(-d{2}(Td{2}:d{2}(:d{2}(.d{2})?)?+d{2}:d{2})?)?)?(?=s|$)/g;
var test:String = "23 1997 1998-07 1995-07s 1937-04-16 " + 
                  "1970-0716 1993-07-16T19:20+01:01 1979-07-16T19:20+0100 " + 
                  "2997-07-16T19:20:30+01:08 3997-07-16T19:20:30.45+01:00";
var result:Object
while(result = regex.exec(test))
    trace(result[0]);

Traced output:

1997
1998-07
1937-04-16
1993-07-16T19:20+01:01
2997-07-16T19:20:30+01:08
3997-07-16T19:20:30.45+01:00

I am using ActionScript here, but the regex should work in most flavors. When implementing it in your language, note that the first and last / are delimiters and the last g stands for global.

问题回答

I d split the input field into many (one for year, month, day etc.).

You can use Javscript to advance from one field to the next once full (i.e. once four characters are in the year box, move focus to month) for smoother entry.

You can then validate each field independently and finally construct the complete date string.





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签