Solved

What string matches either of these regular expressions?

Posted on 2014-02-17
6
376 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 143

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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 143

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sending receiving text messages in vb.net 15 48
Need a simple RegEx to search for two words 27 49
jquery tab header text 1 23
TFS 2015 Access denied. 1 15
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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…

820 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