Solved

What string matches either of these regular expressions?

Posted on 2014-02-17
6
374 Views
Last Modified: 2014-02-17
An in-house web app is asking for a date but errors whatever I put in.

I turned on the debugger and I can see it is trying a couple of regex matches:

^\\s*(\\d{1,2})([-/]|\\. ?)(\\d{1,2})(?:\\s|\\2)((\\d{4})|(\\d{2}))(?:\\s\u0433\\.)?\\s*$

and

^\\s*((\\d{4})|(\\d{2}))([-/]|\\. ?)(\\d{1,2})\\4(\\d{1,2})\\.?\\s*$

I'll put the code below.

I've tried some obvious dates - 2014-02-17 or 02/17/2014 or 02/17/14 but in the arrays generated there always seems to be an element that is null that causes an error in the code.

Can anyone figure out a string that would match one of the expressions?

Here is the code:

    else if (dataType == "Date") {
        var yearFirstExp = new RegExp("^\\s*((\\d{4})|(\\d{2}))([-/]|\\. ?)(\\d{1,2})\\4(\\d{1,2})\\.?\\s*$");
        m = op.match(yearFirstExp);
        var day, month, year;
        if (m != null && (m[2].length == 4 || val.dateorder == "ymd")) {
            day = m[6];
            month = m[5];
            year = (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10))
        }
        else {
            if (val.dateorder == "ymd"){
                return null;
            }
            var yearLastExp = new RegExp("^\\s*(\\d{1,2})([-/]|\\. ?)(\\d{1,2})(?:\\s|\\2)((\\d{4})|(\\d{2}))(?:\\s\u0433\\.)?\\s*$");
            m = op.match(yearLastExp);
            if (m == null) {
                return null;
            }
            if (val.dateorder == "mdy") {
                day = m[3];
                month = m[1];
            }
            else {
                day = m[1];
                month = m[3];
            }
            year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10))
        }
        month -= 1;
        var date = new Date(year, month, day);
        if (year < 100) {
            date.setFullYear(year);
        }
        return (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate()) ? date.valueOf() : null;

Open in new window

0
Comment
Question by:purplesoup
  • 3
  • 2
6 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39864415
in the second one, I would say :
\\4
is wrong, I would put this instead:
([-/]|\\. ?)
0
 

Author Comment

by:purplesoup
ID: 39864420
Thanks - I can't actually change the code, I was just hoping someone might suggest a value that would be accepted by the regex without causing a validation error?
0
 

Author Comment

by:purplesoup
ID: 39864422
Probably it would have been clearer if I had said, instead of

"Can anyone figure out a string that would match one of the expressions?"

this

"Can anyone figure out a string that would match one of the expressions so I could enter the date in the application?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 450 total points
ID: 39864447
I checked with this page:
http://regex101.com/#javascript
where I put this expression (removing the double \\ which is for C# string ...)
^\s*((\d{4})|(\d{2}))([-/]|\. ?)(\d{1,2})\4(\d{1,2})\.?\s*$

the page explains the expression, and typing this:
2014-1-41

it shows that this one matches. the \\4 is to match back the - from the before ...
that is still not a date, but at least a format ...
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 50 total points
ID: 39864465
'11-1-11'
should match both expressions
0
 

Author Closing Comment

by:purplesoup
ID: 39864504
Thanks - I was using a different checker and it wasn't matching - I'll use that one from now on.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Getfiles in vb.net 28 42
Name space syntax error 12 40
Interview questions for support of a legacy ASP.NET site 4 38
C# Single Form 8 27
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now